linux下python指定编码写文件让windows下查看不乱码
程序员文章站
2022-07-13 22:17:46
...
import sys
import chardet
reload(sys)
print sys.getdefaultencoding() >> ascii
sys.setdefaultencoding('utf-8') #重新指定python 的编码格式
print sys.getdefaultencoding() >> utf-8
python 默认的编码是ascii 把他设为 utf-8
f=open("180230-pass.csv",'rb')
data=f.read()
print chardet.detect(data)
with open("180230-pass.csv",'rb') as f:
for line in f:
print line
print sys.getdefaultencoding()
print chardet.detect(line)
line = line.encode("gb2312") ################## 把utf-8编码成gb2312或gbk
#line = line.encode('utf-8').encode("gb2312")
with open("testtt12.txt",'a') as fw:
fw.write(line)
这样长生的文件windows下打开就不会是乱码
import chardet
reload(sys)
print sys.getdefaultencoding() >> ascii
sys.setdefaultencoding('utf-8') #重新指定python 的编码格式
print sys.getdefaultencoding() >> utf-8
python 默认的编码是ascii 把他设为 utf-8
f=open("180230-pass.csv",'rb')
data=f.read()
print chardet.detect(data)
with open("180230-pass.csv",'rb') as f:
for line in f:
print line
print sys.getdefaultencoding()
print chardet.detect(line)
line = line.encode("gb2312") ################## 把utf-8编码成gb2312或gbk
#line = line.encode('utf-8').encode("gb2312")
with open("testtt12.txt",'a') as fw:
fw.write(line)
这样长生的文件windows下打开就不会是乱码
上一篇: aapt 安装
下一篇: numpy的线性方程组和矩阵计算