with open("yesterday","r") as f,\
open("yesterday2","r") as f2: 同時打開兩個文件
for line in f :
print(line)
當with代碼塊執行完畢時,內部會自動關閉並釋放文件資源。
字符編碼:
Unicode 包含GBK和UTF-8
Unicode encode 編碼碼成UTF-8 encode編碼碼成GBK
UTF-8 和GBK decode 編碼成 Unicode
s="你好"
print(s.encode("gbk")) 把s編碼成GBK
s_to_gbk = s.decode("utf-8").encode("gbk")
print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))
編碼成utf-8,解碼成Unicode 再編碼成gb2312 再解碼gb2312成Unicode