lsykkk 发表于 2020-11-18 16:47:32

文本提取

with open('digits.txt') as aa:
    contents=aa.read()
    print(contents)
文件都存储到aa.py的同一个文件夹里了 问什么报错呢

昨非 发表于 2020-11-18 16:52:37

所以报错是啥啊

Twilight6 发表于 2020-11-18 16:57:47


报错建议发下报错内容哈

可能是编码报错吧,在 open 里加上参数 encoding = 'UTF-8'试试看

参考代码:

with open('digits.txt' , encoding = 'UTF-8') as aa:
    contents=aa.read()
    print(contents)

lsykkk 发表于 2020-11-18 17:09:34

Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/aa.py", line 2, in <module>
    contents=aa.read()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
>>>

lsykkk 发表于 2020-11-18 17:28:14

Twilight6 发表于 2020-11-18 16:57
报错建议发下报错内容哈

可能是编码报错吧,在 open 里加上参数 encoding = 'UTF-8'试试看



Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/aa.py", line 2, in <module>
    contents=aa.read()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
>>>

lsykkk 发表于 2020-11-18 17:29:27

昨非 发表于 2020-11-18 16:52
所以报错是啥啊


Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/aa.py", line 2, in <module>
    contents=aa.read()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
>>>

Twilight6 发表于 2020-11-18 17:37:12

lsykkk 发表于 2020-11-18 17:28
Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/aa.py", line 2, in
...


重新保存下 TXT 文本,如果选择 UTF-8 那么就要加上 encoding = 'utf-8' 参数

如果嫌麻烦就直接选择 ANSI 编码报错,ANSI 国内大部分电脑默认 gbk 编码,Python 的open 参数也是默认 gbk 编码 就不用加参数

https://xxx.ilovefishc.com/forum/202005/25/072650zqq1fqqmng421bqz.jpg

https://xxx.ilovefishc.com/forum/202005/25/072647nvzjv2zemvgxet5g.jpg

https://xxx.ilovefishc.com/forum/202006/22/222937li4vv400i6v44b6z.png
页: [1]
查看完整版本: 文本提取