>>>import sys
>>>sys.getdefaultencoding()#'utf-8'
>>>import sys
>>>from imp import reload
>>>reload(sys)
>>>sys.setdefaultencoding('utf-8')#python3无法运行,只能在python2上运行
从外部导入字符串时, 需要将其转换成python易处理的'utf-8'格式 例如:
>>>string.decode('ascii')
当想将'utf-8'字节串写入外部, 并以'utf-8'编码, 而不是windows默认的'gbk' 可直接将'utf-8'的字节串以字节形式写入文本
>>>string = '<span class="title">\xe7\xbe\x8e\xe4\xb8\xbd\xe4\xba\xba\xe7 class="title"> / La vita \xc3\xa8</span'
>>>newString = string.encode("utf-8")#将string转换成“utf-8”的编码,以字节展示
>>>file = open("test.txt", "wb")#以字节形式写入文件
>>>file.write(newString)
>>>file.close()
#文档变成“utf-8”编码的形式