项目紧急交付期加班,耽误学习,今日交付,恢复正常学习。
今日学习内容有:
with的使用,解决忘记close文件或者close文件占用内存的问题:
例子:
with open("A", "r", encoding="utf-8") as f:#或者:with open("A", "r", encoding="utf-8") as f,\ open("A", "r", encoding="utf-8") as 2:
转码:
转码的主要核心在Python2.7和python3.5原理是相同的,不过,python2.7默认是assic,python3.0默认是utf-8
A转为B的流程是:
A decode为unicode,然后encode为B
B decode为unicode,然后encode为A
例子(Python2.7):
#-*- coding:utf-8 -*-a = “你好”a_to_unicode = a.decode("utf-8")#原来为utf-8a_to_gbk = a_to_unicode.encode("gbk")#转为gbk#也可以写作:a_to_gbk = a.decode("utf-8").encode("gbk")
python 3.0与上述原理相同,只不过默认原始编码为unicode,但是默认转码之后格式是bytes二进制格式
_author_ = "Jack Niu"
打印默认编码格式:
import sysprint(sys.getdefaultencoding)