|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位好,
我根据小甲鱼老师的课到了29, 无法顺利执行.read()功能, 显示的错误如下:
>>> f=open('029record.txt')
>>> f
<_io.TextIOWrapper name='029record.txt' mode='r' encoding='US-ASCII'>
>>> f.read()
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
f.read()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
这边好像显示 ascii 无法解码?
请问我应该如何解决呢?
import chardet
tt=open('c:\\111.txt','rb')
ff=tt.readline()
#这里试着换成read(5)也可以,但是换成readlines()后报错
enc=chardet.detect(ff)
print(enc['encoding'])
tt.close()
# 以指定编码打开文件
with open("新建文本文档.txt", "r", encoding=enc) as f:
print(f.read())
|
|