利用python读取带有中文的字符串,和将带有中文的字符串写到txt文件中

来源:互联网 发布:netbsd源码 编辑:程序博客网 时间:2024/05/20 18:00

从txt文件中读入

filePath为文件路径,encoding='utf-8'表示读进来来后以utf-8进行编码
f = open(filePath,encoding='utf-8')


写到txt文件中:

用的是python3.5版本:
如果只是使用系统默认的open()方法,只能写入ascii码,要读入中文,可以使用codecs模块:


 import codecs
return=u"中文"
file = codecs.open(filePatn,'w','utf-8')

0 0