在 Python 上读取文件时,出现 UnicodeDecodeError。我能做些什么来解决这个问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16528468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:56:05  来源:igfitidea点击:

While reading file on Python, I got a UnicodeDecodeError. What can I do to resolve this?

pythonfile-iopython-3.x

提问by Bugboy1028

This is one of my own projects. This will later help benefit other people in a game I am playing (AssaultCube). Its purpose is to break down the log file and make it easier for users to read.

这是我自己的项目之一。这将有助于在我正在玩的游戏(AssaultCube)中让其他人受益。其目的是分解日志文件,方便用户阅读。

I kept getting this issue. Anyone know how to fix this? Currently, I am not planning to write/create the file. I just want this error to be fixed.

我一直收到这个问题。有人知道怎么修这个东西吗?目前,我不打算编写/创建文件。我只想修复这个错误。

The line that triggered the error is a blank line (it stopped on line 66346).

触发错误的行是一个空行(它停在第 66346 行)。

This is what the relevant part of my script looks like:

这是我脚本的相关部分的样子:

log  =  open('/Users/Owner/Desktop/Exodus Logs/DIRTYLOGS/serverlog_20130430_00.15.21.txt', 'r')
for line in log:

and the exception is:

例外是:

Traceback (most recent call last):
  File "C:\Users\Owner\Desktop\Exodus Logs\Log File Translater.py", line 159, in <module>
    main()
 File "C:\Users\Owner\Desktop\Exodus Logs\Log File Translater.py", line 7, in main
    for line in log:
  File "C:\Python32\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 0x81 in position 3074: character maps to <undefined>

采纳答案by Rouven B.

Try:

尝试:

enc = 'utf-8'
log = open('/Users/Owner/Desktop/Exodus Logs/DIRTYLOGS/serverlog_20130430_00.15.21.txt', 'r', encoding=enc)

if it won't work try:

如果它不起作用尝试:

enc = 'utf-16'
log = open('/Users/Owner/Desktop/Exodus Logs/DIRTYLOGS/serverlog_20130430_00.15.21.txt', 'r', encoding=enc)

you could also try it with

你也可以试试

enc = 'iso-8859-15'

also try:

也可以尝试:

enc = 'cp437'

wich is very old but it also has the "ü" at 0x81 wich would fit to the string "ü?er" wich I found on the homepage of assault cube.

这是非常古老的,但它在 0x81 处也有“ü”,这将适合我在攻击立方体的主页上找到的字符串“ü?er”。

If all the codings are wrong try to contact some of the guys developing assault cube or as mentioned in a comment: have a look at https://pypi.python.org/pypi/chardet

如果所有编码都错误,请尝试联系一些开发攻击立方体的人或如评论中所述:查看https://pypi.python.org/pypi/chardet