Python unicode 解码错误 SUD
好的,所以我在脚本顶部有 # -*-coding: utf-8 -*-
,它可以从具有有趣字符的数据库中提取数据(Ñ,Õ, é,—,–,',...) 并将该数据存储到变量中......但我遇到了其他问题,看到我提取数据,组织它,然后将其转储到变量中,如下所示
title = product[1]
: code>product[1] 来自我的数据库结果集
然后我将其加载为 Suds像这样:
array_of_inventory_item_submit = ca_client_inventory.factory.create('ArrayOfInventoryItemSubmit')
for product in products:
inventory_item_submit = ca_client_inventory.factory.create('InventoryItemSubmit')
inventory_item_list = get_item_list(product)
inventory_item_submit = [inventory_item_list]
array_of_inventory_item_submit.InventoryItemSubmit.append(inventory_item_submit)
#Call that service baby!
ca_client_inventory.service.SynchInventoryItemList(accountID, array_of_inventory_item_submit)
其中 get_item_list
将 product[1]
设置为标题并且(包括一大堆其他节点):
inventory_item_submit.Title = title
所以一切都运行良好,直到我调用 ca_client_inventory.service.SynchInventoryItemList包含 array_of_inventory_item_submit
其中包含带有时髦字符的标题...这是错误:
Traceback (most recent call last):
File "upload_all_inventory_ebay.py", line 421, in <module>
ca_client_inventory.service.SynchInventoryItemList(accountID, array_of_inventory_item_submit)
File "build/bdist.macosx-10.6-i386/egg/suds/client.py", line 539, in __call__
File "build/bdist.macosx-10.6-i386/egg/suds/client.py", line 592, in invoke
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/binding.py", line 118, in get_message
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/document.py", line 63, in bodycontent
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/document.py", line 105, in mkparam
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/binding.py", line 260, in mkparam
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 62, in process
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 243, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 298, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 298, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 243, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 198, in append
File "build/bdist.macosx-10.6-i386/egg/suds/sax/element.py", line 251, in setText
File "build/bdist.macosx-10.6-i386/egg/suds/sax/text.py", line 43, in __new__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 116: ordinal not in range(128)
现在怎么办?我的猜测是我的脚本可以接受这些时髦的字符,因为我在顶部有 # -*-coding: utf-8 -*-
但 Suds 的文件顶部没有这个。我真的想要去更改 Suds 文件吗...我们都知道这是最不希望的最后可能的解决方案...我能做什么?
OK so I have # -*- coding: utf-8 -*-
at the top of my script and it worked for being able to pull data from the database that had funny chars(Ñ ,Õ,é,—,–,’,…) in it and store that data into variables...but I have run into other problems, see I pull my data, organize it, and then dump it into a variables like so:
title = product[1]
Where product[1]
is from my database result set
Then I load it up for Suds like so:
array_of_inventory_item_submit = ca_client_inventory.factory.create('ArrayOfInventoryItemSubmit')
for product in products:
inventory_item_submit = ca_client_inventory.factory.create('InventoryItemSubmit')
inventory_item_list = get_item_list(product)
inventory_item_submit = [inventory_item_list]
array_of_inventory_item_submit.InventoryItemSubmit.append(inventory_item_submit)
#Call that service baby!
ca_client_inventory.service.SynchInventoryItemList(accountID, array_of_inventory_item_submit)
Where get_item_list
sets product[1]
to title and (including a whole bunch of other nodes):
inventory_item_submit.Title = title
So everything runs fine until I call ca_client_inventory.service.SynchInventoryItemList that contains array_of_inventory_item_submit
which contains the title w/ the funky char...here is the error:
Traceback (most recent call last):
File "upload_all_inventory_ebay.py", line 421, in <module>
ca_client_inventory.service.SynchInventoryItemList(accountID, array_of_inventory_item_submit)
File "build/bdist.macosx-10.6-i386/egg/suds/client.py", line 539, in __call__
File "build/bdist.macosx-10.6-i386/egg/suds/client.py", line 592, in invoke
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/binding.py", line 118, in get_message
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/document.py", line 63, in bodycontent
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/document.py", line 105, in mkparam
File "build/bdist.macosx-10.6-i386/egg/suds/bindings/binding.py", line 260, in mkparam
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 62, in process
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 243, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 298, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 298, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 243, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 182, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/core.py", line 75, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 102, in append
File "build/bdist.macosx-10.6-i386/egg/suds/mx/appender.py", line 198, in append
File "build/bdist.macosx-10.6-i386/egg/suds/sax/element.py", line 251, in setText
File "build/bdist.macosx-10.6-i386/egg/suds/sax/text.py", line 43, in __new__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 116: ordinal not in range(128)
Now what? My guess is my script can take in these funky chars because I have # -*- coding: utf-8 -*-
at the top but Suds does NOT have that at the top of its files. Do I really want to go and change the Suds files...we all know this is the least desired last possible solution...what can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#-*-coding: xxx -*-
与此错误无关,它仅适用于声明它的源文件的编码,而不是内容来自数据库的变量。您的错误表明您尝试将包含非 ASCII 字符的
str
类型对象传递给unicode()
构造函数(在 suds/sax/text.py 的第 43 行)。您必须将来自数据库的字符串转换为
unicode
对象;例如,如果您的数据库以 UTF-8 编码:请参阅 str.decode() 文档了解详细信息。
#-*- coding: xxx -*-
has nothing to do with this error, it only applies to the encoding of the source file it is declared in, not the content of variables coming from a database.Your error says that you try to pass a
str
type object containing non ASCII characters to theunicode()
constructor (which is called at line 43 of suds/sax/text.py).You have to convert the strings coming from the database to
unicode
objects ; for example if your database is encoded in UTF-8:See the str.decode() documentation for details.