The WITH statement enables automatic file shutdown:
1 ImportSYS2 " "3 With open ("Yesterday2", "R", encoding= "Utf-8") as F: #自动关闭并释放文件资源4 For line in F:5 print (line)6 " "7With open ("Yesterday2","R", encoding="Utf-8") as F,8Open"Yesterday2","R", encoding="Utf-8") as F2:9 forLineinchF:Ten Print(line)
Character transcoding: https://www.cnblogs.com/jxzheng/p/5186490.html
Https://www.cnblogs.com/nulige/p/6063999.html
1s ="Hello" #default Unicode2S_TO_GBK =s.encode ("GBK")#Unicode transcoding into GBK3 Print(S_TO_GBK)4 5 Print(S_to_gbk.decode ("GBK"))#Transcoding from GBK to Unicode6 7 Print(S.encode ("GBK"))8 Print(S.encode ("Utf-8"))9 Print(S.encode ("Utf-8"). Decode ("Utf-8"). Encode ("gb2312"))Ten One ImportSYS A Print(Sys.getdefaultencoding ())#Fetch system default encoding
Python Learning path: file with auto-close + character transcoding