【发布时间】:2014-03-11 05:52:26
【问题描述】:
我试图打开一个文件,但不断收到错误消息(解码错误)。我终于在stackoverflow上遇到了一个话题,并且更进一步。但是,现在当我尝试遍历我的文件时,我没有得到任何结果。该文件是 uTorrent 的设置文件。我对 Python 和编程相当陌生,所以我想我可以尝试从该文件中读取和提取一些参数。
getDir = r'{0}/{1}/{2}/AppData/Roaming/uTorrent/settings.dat'.format(partition,
"Users", user)
data = []
try:
with codecs.open(getDir, "r") as f:
for lines in f:
data.append(lines)
except UnicodeEncodeError:
pass
我在使用 UnicodeEncodeError 时遇到错误,但在其他异常情况下没有。我尝试过使用/不使用 codecs.open 并在 open 函数中指定解码类型,我也使用了 ignore。代码通过,但列表仍然为空。
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1962: character maps to <undefined>
该代码适用于同一目录中的其他文件。我检查了文件,有一些奇怪的字符。下面的例子。
RÉ” tÝï:Q‘æÐ†•
非常感谢您的帮助,并感谢您提供了一个很棒的网站!
【问题讨论】:
-
您没有指定目标编码。你知道是哪一个吗?
-
不,请告诉我我是怎么做到的。
标签: python eclipse python-3.x