suchen

Heim  >  Fragen und Antworten  >  Hauptteil

python 如何读取windows-1252格式文本?

这是一个GPS log文件,编码方式看sublimetext显示是windows-1252格式
][1]

我想读取这个文件一行打印用下面代码:

#!user/bin/python3

fin=open('gpsdebug.log.20100101000042.c','rt',encoding="windows-1252")
lines = fin.readline()
fin.close()
print(lines)

提示报错:

Traceback (most recent call last):
  File "strline.py", line 8, in <module>
    lines = fin.readline()
  File "/usr/lib/python3.5/encodings/cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 3504: character maps to <undefined>

不知道问题出在哪里?

补充:
删除文本文档中一部分乱码字符可以正常读取,如果不处理这些字符有办法跳过吗?

PHP中文网PHP中文网2885 Tage vor909

Antworte allen(1)Ich werde antworten

  • 怪我咯

    怪我咯2017-04-18 10:13:19

    fin=open('gpsdebug.log.20100101000042.c','rt',encoding="windows-1252",errors='ignore')

    Antwort
    0
  • StornierenAntwort