第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Python的文件操作

標(biāo)簽:
Python

1、 读写文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/25 20:49
# @Author  : zhouyuyao
# @File    : demonWrite.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32
if __name__== "__main__":
    filename = input("Please input the name of file:")
    f = open(filename,"w")     # 以写的形式打开一个文件
    while 1:         # 1 的效率是最高的
        context = input("Please input context('EOF' will close file): ")
        if context == "EOF":
            f.close()
            break
        else:
            f.write(context)
            f.write("\n")
    fRead = open(filename)
    readContext = fRead.read()
    print("------------start-------------")
    print(readContext)
    print("-------------end--------------")
    fRead.close()

运行结果:

Please input the name of file:z.log
Please input context('EOF' will close file): hello
Please input context('EOF' will close file): the weather is cool
Please input context('EOF' will close file): you have wear more clothes
Please input context('EOF' will close file): EOF
------------start-------------
hello
the weather is cool
you have wear more clothes
-------------end--------------

2、 读取文件方法

import codecs
ENCODING = "utf-8"       # 字符集
f = open("z.log",encoding=ENCODING)
print(f.name)            # 文件名
print(f.readline())      # 读取成列表的形式
print(f.readlines())     # 读取成列表的形式
with codecs.open("z.log","r",encoding=ENCODING) as f:
    print(f.read())

3、 编码问题

编码:
支持中文的编码:utf-8,gbk,gb2312

decode  解码
encode  编码

在Python2中不定义代码的编码排头,在内容中出现中文时会报错。
Python默认将代码文件内容当做ASCII编码处理,但是ASCII编码不存在中文,因为则会抛出异常。
解决问题之道就是要让Python之道文件中使用的是什么编码形式,对于中文,可以用的常见编码有utf-8,gbk和gb2312等,只需在代码文件的最前端添加如下内容即可:

# -*- coding:utf-8 -*-

Python转码的过程:
原有编码 ——> Unicode编码 ——> 目的编码

python会自动将带中文的字符串解码成Unicode,然后再编码成gbk,因为解码是字典进行的,如果没有指明解码方式,就会使用sys,defaultencoding指明的方式来解码。
方法一:
s.decode("utf-8").encoding("gbk")

4、对文件进行排序

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/25 23:06
# @Author  : zhouyuyao
# @File    : sortUIDPasswd.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32
import codecs
file = "passwd"
sortfile = "sortpasswd.txt"
filecontext = []
sortuid = []
with codecs.open(sortfile,"wb") as fsort:
    with codecs.open(file,encoding="utf-8") as f:
        filecontext += f.readlines()
        for line in filecontext:
            sortuid.append(int(line.split(":")[2]))
        sortuid.sort()
        for uid in sortuid:
            for line in filecontext:
                if str(uid) == line.split(":")[2]:
                    print(line)
                    fsort.write(line.encode("utf-8"))

python3的新特性对文本和二进制数据作了更为清晰的区分,
文本总是Unicode,由str类型表示,
二进制则是由bytes类型表示
字符串可以encode编码成字节包,而字节包可以decode解码成字符串

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消