您现在的位置是:课程教程文章
python f.write中文乱码怎么解决
2023-12-18 22:25课程教程文章 人已围观

举个例子:
#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中文乱码怎么解决
上一篇:python如何计算时间差
下一篇:没有了