仅在使用 cx_freeze 时出现 UnicodeDecodeError

发布于 2024-11-05 09:51:32 字数 300 浏览 0 评论 0原文

当我在冻结脚本后尝试运行程序时,出现错误:“UnicodeDecodeError:'ascii'编解码器无法解码位置7338中的字节0xa0:序号不在范围(128)” cx_freeze。如果我正常运行 Python 3 脚本,它运行得很好,但只有在我冻结它并尝试运行可执行文件后,它才会给我这个错误。我会发布我的代码,但我不知道要发布哪些部分,所以如果有任何某些部分可以帮助我知道,我会发布它们,否则似乎我以前遇到过这个问题并解决了它,但已经有一段时间了,我不记得问题到底是什么或我如何解决它,所以任何帮助或指示让我朝着正确的方向前进将有很大帮助。提前致谢。

I get the error: "UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 7338: ordinal not in range(128)" once I try to run the program after I freeze my script with cx_freeze. If I run the Python 3 script normally it runs fine, but only after I freeze it and try to run the executable does it give me this error. I would post my code, but I don't know exactly what parts to post so if there are any certain parts that will help just let me know and I will post them, otherwise it seems like I have had this problem once before and solved it, but it has been a while and I can't remember what exactly the problem was or how I fixed it so any help or pointers to get me going in the right direction will help greatly. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野の 2024-11-12 09:51:32

准确告诉我们哪个版本的 Python 在哪个平台上。

显示错误发生时获得的完整回溯。你自己看看吧。出现的代码的最后一行是什么?您认为正在解码的 bytes 字符串是什么?为什么使用 ascii 编解码器?

请注意,Python 3.x 不会使用默认编解码器(例如 ascii)自动将 bytes 转换为 str。所以要么你明确地这样做,要么 cx_freeze 这样做。

在评论中提供更多信息后更新

Excel 不以 ASCII 格式保存 csv 文件。它将它们保存在 MS 所谓的“ANSI 代码页”中,该代码页因区域设置而异。如果您不知道自己的是什么,它可能是cp1252。要进行检查,请执行以下操作:

>>> import locale; print(locale.getpreferredencoding())
cp1252

如果 Excel 确实以 ASCII 保存文件,则有问题的 '\xa0' 字节将被替换为 '?'并且您不会收到 UnicodeDecodeError。

UTF-8 格式保存文件需要您使用 encoding='utf8' 打开文件,并且会遇到同样的问题(除了您会收到有关0xc2 而不是 0xa0)。

您无需将所有四个 csv 文件发布到网络上。只需运行这个小脚本(未经测试):

import sys
for filename in sys.argv[1:]:
    for lino, line in enumerate(open(filename), 1):
        if '\xa0' in line:
            print(ascii(filename), lino, ascii(line))

'\xa0' 是一个 NO-BREAK SPACE 又名   ...可能需要编辑您的文件以将其更改为普通空间。

您可能需要在 cx_freeze 邮件列表上询问才能获得发生此错误的原因的答案。他们会想知道完整的追溯。进行一些练习 - 在这里展示。

顺便说一句,“offset 7338”相当大——您期望 csv 文件中的行那么长吗?也许有些东西正在读取您的所有文件......

Tell us exactly which version of Python on what platform.

Show the full traceback that you get when the error happens. Look at it yourself. What is the last line of your code that appears? What do you think is the bytes string that is being decoded? Why is the ascii codec being used??

Note that automatic conversion of bytes to str with a default codec (e.g. ascii) is NOT done by Python 3.x. So either you are doing it explicitly or cx_freeze is.

Update after further info in comments.

Excel does not save csv files in ASCII. It saves them in what MS calls "the ANSI codepage", which varies by locale. If you don't know what yours is, it is probably cp1252. To check, do this:

>>> import locale; print(locale.getpreferredencoding())
cp1252

If Excel did save files in ASCII, your offending '\xa0' byte would have been replaced by '?' and you would not be getting a UnicodeDecodeError.

Saving your files in UTF-8 would need you to open your files with encoding='utf8' and would have the same problem (except that you'd get a grumble about 0xc2 instead of 0xa0).

You don't need to post all four of your csv files on the web. Just run this little script (untested):

import sys
for filename in sys.argv[1:]:
    for lino, line in enumerate(open(filename), 1):
        if '\xa0' in line:
            print(ascii(filename), lino, ascii(line))

The '\xa0' is a NO-BREAK SPACE aka   ... you may want to edit your files to change these to ordinary spaces.

Probably you will need to ask on the cx_freeze mailing list to get an answer to why this error is happening. They will want to know the full traceback. Get some practice -- show it here.

By the way, "offset 7338" is rather large -- do you expect lines that long in your csv file? Perhaps something is reading all of your file ...

初雪 2024-11-12 09:51:32

该错误本身表明 python 字符串中的字符不是正常的 ASCII 字符:

>>> b'abc\xa0'.decode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 3: ordinal not in range(128)

我当然不知道为什么只有在脚本被冻结时才会发生这种情况。您可以将整个脚本包装在 try/except 中,并手动打印出相关字符串的全部或部分。

编辑:这可能是这样的

try:
    # ... your script here
except UnicodeDecodeError as e:
    print("Exception happened in string '...%s...'"%(e.object[e.start-50:e.start+51],))
    raise

That error itself indicates that you have a character in a python string that isn't a normal ASCII character:

>>> b'abc\xa0'.decode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 3: ordinal not in range(128)

I certainly don't know why this would only happen when a script is frozen. You could wrap the whole script in a try/except and manually print out all or part of the string in question.

EDIT: here's how that might look

try:
    # ... your script here
except UnicodeDecodeError as e:
    print("Exception happened in string '...%s...'"%(e.object[e.start-50:e.start+51],))
    raise
公布 2024-11-12 09:51:32

通过设置默认编码修复:

reload(sys)
sys.setdefaultencoding("utf-8")

fix by set default coding:

reload(sys)
sys.setdefaultencoding("utf-8")
○闲身 2024-11-12 09:51:32

对这些行使用 str.decode() 函数。您还可以指定编码,例如 myString.decode('cp1252')

另请参阅: http://docs.python.org/ release/3.0.1/howto/unicode.html#unicode-howto

Use str.decode() function for that lines. And also you can specify encoding like myString.decode('cp1252').

Look also: http://docs.python.org/release/3.0.1/howto/unicode.html#unicode-howto

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文