Discuss / Python / 如果txt文件里是中文的话该怎么办?

如果txt文件里是中文的话该怎么办?

Topic source

BRLIALmm

#1 Created at ... [Delete] [Delete and Lock User]

我的txt文件里是“相信马总相信爱”这一句话,然后程序如下

-- coding: utf-8 --

fpath = r'D:\pytraining\strth.txt'

with open(fpath, 'r',encoding='gb18030',errors='ignore') as f: s = f.read() print(s)

一开始全部时候gbk在报错,然后上网查了很多文章,吧gbk改成gb8030,加上ignore后结果print函数有问题: Traceback (most recent call last): File "C:\Users\ADMINI~1\AppData\Local\Temp\learn_python_63_6vino_py\test_20.py", line 6, in <module> print(s) UnicodeEncodeError: 'gbk' codec can't encode character '\ue101' in position 4: illegal multibyte sequence

这是为什么呢?该怎么改呢?求各位大神赐教

hevi-ve

#2 Created at ... [Delete] [Delete and Lock User]

这文件是utf8格式的吧


  • 1

Reply