编码问题:python写入文件

 

方法一:(推荐)

line1 = "我爱中国111"
line2 = u"我爱祖国222"

with open('1.txt','w',encoding='utf-8') as f:
    f.write(line2)

  

 

方法二:codecs

http://blog.csdn.net/mbugatti/article/details/53925236

import codecs

line1 = "我爱中国111"
line2 = u"我爱祖国222"

with codecs.open('1.txt', 'w', 'utf-8')as f:
    f.write(line1)

  

 

转载于:https://www.cnblogs.com/andy9468/p/8258242.html

你可能感兴趣的:(编码问题:python写入文件)