標籤:python /usr int tar adl 3.4 操作 效率 passwd
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 win32if __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 codecsENCODING = "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、 Python的編碼問題
編碼:
支援中文的編碼: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、 Python對passwd檔案進行排序
#!/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 win32import codecsfile = "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解碼成字串
Python的檔案操作