# -*- coding:utf-8 -*- china=u'我爱你,中国' #china=china.encode('utf8') file_txt = open("zhongguo.txt", "wb") file_txt.write(china) file_txt.close()
python2 将一个unicode编码的字符串写入到文件中,会报错。
那么,就需要给unicode字符串转换成utf8.
# -*- coding:utf-8 -*- china=u'我爱你,中国' #china=china.encode('utf8') file_txt = open("zhongguo.txt", "wb") file_txt.write(china) file_txt.close()
python2 将一个unicode编码的字符串写入到文件中,会报错。
那么,就需要给unicode字符串转换成utf8.
转载于:https://www.cnblogs.com/taoyuanming/p/10825862.html