教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python f.write中文乱码怎么解决

python f.write中文乱码怎么解决

发布时间:2021-05-03   编辑:jiaochengji.com
教程集为您提供python f.write中文乱码怎么解决等资源,欢迎您收藏本站,我们将为您提供最新的python f.write中文乱码怎么解决资源

举个例子:

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

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

相关推荐:《Python基础教程》

解决方案一:

#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中文乱码怎么解决
php网页标题中文乱码如何解决
Python中文乱码怎么办
执行python脚本出现乱码怎么解决
python command乱码怎么解决
Python之JSON函数介绍
ultraedit python乱码怎么解决
python json中文乱码怎么解决
python 获取网页乱码怎么解决
python open函数中文乱码怎么解决

[关闭]
~ ~