仅在使用 cx_freeze 时出现 UnicodeDecodeError
当我在冻结脚本后尝试运行程序时,出现错误:“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
准确告诉我们哪个版本的 Python 在哪个平台上。
显示错误发生时获得的完整回溯。你自己看看吧。出现的代码的最后一行是什么?您认为正在解码的
bytes
字符串是什么?为什么使用ascii
编解码器?请注意,Python 3.x 不会使用默认编解码器(例如 ascii)自动将
bytes
转换为str
。所以要么你明确地这样做,要么 cx_freeze 这样做。在评论中提供更多信息后更新。
Excel 不以 ASCII 格式保存 csv 文件。它将它们保存在 MS 所谓的“ANSI 代码页”中,该代码页因区域设置而异。如果您不知道自己的是什么,它可能是
cp1252
。要进行检查,请执行以下操作:如果 Excel 确实以 ASCII 保存文件,则有问题的
'\xa0'
字节将被替换为 '?'并且您不会收到 UnicodeDecodeError。以
UTF-8
格式保存文件需要您使用encoding='utf8'
打开文件,并且会遇到同样的问题(除了您会收到有关0xc2 而不是 0xa0)。您无需将所有四个 csv 文件发布到网络上。只需运行这个小脚本(未经测试):
'\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 theascii
codec being used??Note that automatic conversion of
bytes
tostr
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: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 withencoding='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):
The
... you may want to edit your files to change these to ordinary spaces.
'\xa0'
is aNO-BREAK SPACE
akaProbably 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 ...
该错误本身表明 python 字符串中的字符不是正常的 ASCII 字符:
我当然不知道为什么只有在脚本被冻结时才会发生这种情况。您可以将整个脚本包装在
try
/except
中,并手动打印出相关字符串的全部或部分。编辑:这可能是这样的
That error itself indicates that you have a character in a python string that isn't a normal ASCII character:
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
通过设置默认编码修复:
fix by set default coding:
对这些行使用
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 likemyString.decode('cp1252')
.Look also: http://docs.python.org/release/3.0.1/howto/unicode.html#unicode-howto