码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-09-29 20:58:29      阅读:314      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!