python 如何读取windows-1252格式文本?
PHP中文网
PHP中文网 2017-04-18 10:11:19
0
1
683

这是一个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  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 

不知道问题出在哪里?

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

PHP中文网
PHP中文网

认证0级讲师

reply all (1)
刘奇

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

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!