首页 > 编程语言 > 详细

【python】f.write()写入中文出错解决办法

时间:2016-09-29 20:58:20      阅读:377      评论:0      收藏:0      [点我收藏+]

一个出错的例子

#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()

 

【python】f.write()写入中文出错解决办法

原文:http://www.cnblogs.com/dplearning/p/5920912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!