一个出错的例子

#coding:utf-8
s = u'中文'
f = open("test.txt","w")
f.write(s)
f.close()

 

原因是编码方式错误,应该改为utf-8编码

 

解决方案一:

#coding:utf-8
s = u'中文'
f = open("test.txt","w")
f.write(s.encode("utf-8"))
f.close()

 

解决方案二:

#coding:utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf-8') 

s = u'中文'
f = open("test.txt","w")
f.write(s)
f.close()

 

相关文章:

  • 2022-12-23
  • 2021-04-12
  • 2021-07-15
  • 2021-10-01
  • 2022-12-23
  • 2021-12-10
  • 2021-12-05
  • 2021-10-09
猜你喜欢
  • 2021-07-16
  • 2021-12-01
  • 2021-11-03
  • 2021-09-29
  • 2021-08-15
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案