python - 无法使用 Python 3 读取和编辑文件
问题描述
这是我尝试过的:
>>> with open("symbols.raw") as f:
... text=f.readlines()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Python35\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 1694: character maps to <undefined>
>>> with open("symbols.raw",encoding='utf-16') as f:
... text=f.readlines()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Python35\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
File "C:\Python35\lib\encodings\utf_16.py", line 61, in _buffer_decode
codecs.utf_16_ex_decode(input, errors, 0, final)
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 7500-7501: illegal encoding
>>> with open("symbols.raw",encoding='utf-8') as f:
... text=f.readlines()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Python35\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 7: invalid start byte
当我尝试使用二进制模式时,它被加载但我无法理解如何读取和编辑我自己的数据。
>>> with open("symbols.raw",'rb') as f:
... text=f.readlines()
...
这是文件:symbol.raw
请让我知道如何以人类解释的方式阅读它并在其中写入我自己的数据。这是symbols.raw 文件的格式。
解决方案
您可以使用encoding="ISO-8859-1"
:
with open("symbols.raw", encoding="ISO-8859-1") as f:
text=f.readlines()
推荐阅读
- javascript - 如何在被点击的元素上执行 EventListener 脚本
- android-studio - 从 android studio 中删除不需要的弹出通知
- android - Android 底部导航活动在 onCreate 中给出空指针异常
- java - 获取列表中具有特定泛型类型的元素
- python-3.x - 连线 ttk Label textvariable 无法在类中正确显示
- mysql - 我该如何解决这个问题?使用自内连接和一些条件?
- java - 很多依赖都指向了:找不到类 [org.neo4j.ogm.session.SessionFactory] 并且无法将其添加到 pom.xml 导致无法找到
- javascript - 如何在没有硬编码的情况下使用多个不和谐机器人帐户登录?
- python - 如何修复graphviz(和PATH)的执行错误?
- python - 有没有办法检查python中的拼写错误?