首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnicodeDecodeError在SUDS中,但仅在py2exe生成的.exe中。

UnicodeDecodeError在SUDS中,但仅在py2exe生成的.exe中。
EN

Stack Overflow用户
提问于 2011-09-08 14:57:11
回答 1查看 826关注 0票数 2

我有一个Python2.7脚本SendPreord.py,它使用SUDS与web服务进行通信。在脚本中,我调用一个web服务方法,将一些参数作为字符串 (runJob(par1, par2, par3))传递。它能很好地处理字符串中的西欧字符。我在Eclipse中使用PyDev运行它。

然后,我使用.exe生成py2exe。现在它给了我错误

代码语言:javascript
复制
Traceback (most recent call last):
  File "SendPreord.py", line 80, in <module>
  File "suds\client.pyc", line 542, in __call__
  File "suds\client.pyc", line 602, in invoke
  File "suds\client.pyc", line 637, in send
  File "suds\transport\https.pyc", line 64, in send
  File "suds\transport\http.pyc", line 77, in send
  File "suds\transport\http.pyc", line 118, in u2open
  File "urllib2.pyc", line 391, in open
  File "urllib2.pyc", line 409, in _open
  File "urllib2.pyc", line 369, in _call_chain
  File "urllib2.pyc", line 1173, in http_open
  File "urllib2.pyc", line 1142, in do_open
  File "httplib.pyc", line 946, in request
  File "httplib.pyc", line 987, in _send_request
  File "httplib.pyc", line 940, in endheaders
  File "httplib.pyc", line 801, in _send_output
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 484: ordinal not in range(128)

导致错误的代码是:

代码语言:javascript
复制
result = ws_client.service.runJob(par1, par2, par3)

经过调查,我意识到删除像°èòà这样的字符可以解决这个问题.可是,我不会呀!我必须保留我要传递的字符串。

因此,在传递字符串之前,我尝试了对它们进行解码:

代码语言:javascript
复制
result = ws_client.service.runJob(par1.decode('latin9'), par2.decode('latin9'), par3.decode('latin9'))

同样,全部在.py中工作,但在.exe中不起作用。也许PyDev在某种程度上纠正了这个问题?

附件

Setup.py:

代码语言:javascript
复制
from distutils.core import setup
import py2exe
setup(console=['src/SendPreord.py'])

py2exe输出日志中有趣的摘录:

代码语言:javascript
复制
*** copy dlls ***
copying C:\Python27\lib\site-packages\py2exe\run.exe -> C:\Users\xxxxxxx\workspace\eclipse\SendPreord\dist\SendPreord.exe
The following modules appear to be missing
['ElementC14N', '_scproxy', 'ntlm']

*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.

Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.

   USER32.dll - C:\Windows\system32\USER32.dll
   SHELL32.dll - C:\Windows\system32\SHELL32.dll
   WSOCK32.dll - C:\Windows\system32\WSOCK32.dll
   ADVAPI32.dll - C:\Windows\system32\ADVAPI32.dll
   WS2_32.dll - C:\Windows\system32\WS2_32.dll
   KERNEL32.dll - C:\Windows\system32\KERNEL32.dll
EN

回答 1

Stack Overflow用户

发布于 2011-09-08 19:49:27

Python在编码转换方面的猜测已经让你被咬了。您尝试过的第一部分是正确的:首先使用(希望是正确的)编码解码。在发送之前,您必须再次对其进行编码,最好使用类似于UTF-8的代码,否则Python会尝试“默认”编码(在大多数安装中是ASCII)。I've written this here before

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7350115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档