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解碼成字元串