python读文件的解码方式_跳过Python文件读取中的不可解码字符
我有一個csv文件,我想用Python讀取它。當我使用下面的代碼片段時,我得到一個錯誤。在with open(input_file, 'r') as file:
self.md = file.read()
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 58658: ordinal not in range(128)
或者
^{pr2}$
或者with open(input_file, 'r', encoding='utf8') as file:
self.md = file.read()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 58658: invalid start byte
或者with open(input_file, 'r') as file:
# START INFINITE LOOP
while (True):
self.md = file.readline()
print (self.md)
# END INFINITE LOOP
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 1314: invalid start byte
當我在textwarangler或Excel中打開該文件時,即使選擇了displayinfisibles in textwarangler,我也沒有看到任何奇怪的字符。其他一些奇怪的觀察:出錯的總是第1380行,即使我從文件中刪除了1370-1390行。這讓我懷疑這一行是否有錯別字。在
有沒有一種方法可以讀取文件并直接跳過不可解碼的字符?在
編輯
這是問題區域周圍的一個十六進制垃圾場。位置58658是十六進制的位置E522。二線第二場的89人似乎是罪魁禍首。在000e510: 3436 3822 3b22 4152 454d 4920 2020 2020 468";"AREMI
000e520: 6e6f 8922 3b3b 3b0a 2246 3130 3030 3134 no.";;;."F100014
000e530: 3639 223b 2230 3030 3134 3639 223b 2245 69";"0001469";"E
編輯2
{cd1>編碼可以讀取文件。問題仍然存在:是否可以假設UTF-8讀取文件,并跳過無法讀取的字節序列?在
總結
以上是生活随笔為你收集整理的python读文件的解码方式_跳过Python文件读取中的不可解码字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机械键盘换轴要多少钱一个
- 下一篇: 周怎么换算成月_您每天需要多少能量,怎么