UnicodeDecodeError仅与cx_freeze [英] UnicodeDecodeError only with cx_freeze

查看:81
本文介绍了UnicodeDecodeError仅与cx_freeze的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息: 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.

推荐答案

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

Tell us exactly which version of Python on what platform.

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

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??

请注意,自动转换 bytes <使用默认编解码器(例如ascii)将/ code>转换为 str 并非由Python 3.x完成。因此,您在显式执行此操作还是cx_freeze正在执行。

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不会将CSV文件保存为ASCII。它将它们保存在MS所说的 ANSI代码页中,该代码页因地区而异。如果您不知道自己的名字,可能是 cp1252 。要进行检查,请执行以下操作:

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

如果Excel确实将文件保存为ASCII,您冒犯的'\xa0'字节将被替换为'?',并且不会出现UnicodeDecodeError。

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

将文件保存在 UTF-8 中将需要您使用 encoding ='utf8'打开文件,然后会有相同的问题(除了您会抱怨0xc2而不是0xa0)。

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).

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

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))

'\xa0'无间断空格 aka & nbsp; ...您可能希望编辑文件以将其更改为普通文件空格。

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

可能您需要在cx_freeze邮件列表上询问,以获取发生此错误的答案。他们将想知道完整的追溯。练习一下-在此处显示。

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.

顺便说一句,偏移量7338相当大-您期望csv文件中的行长吗?也许某件事正在读取您的所有文件...

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 ...

这篇关于UnicodeDecodeError仅与cx_freeze的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆