3

好的,所以我# -*- coding: utf-8 -*-在脚本的顶部,它能够从数据库中提取包含有趣字符(Ñ,Õ,é,-,-,',...)的数据并将该数据存储到变量中。 ..但我遇到了其他问题,请参阅我提取数据,组织它,然后将其转储到如下变量中:

title = product[1]

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)

Whereget_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 文件吗……我们都知道这是最不希望的最后可能的解决方案……我该怎么办?

4

1 回答 1

10

#-*- coding: xxx -*-与此错误无关,它仅适用于声明它的源文件的编码,而不适用于来自数据库的变量的内容。

您的错误表明您尝试将str包含非 ASCII 字符的类型对象传递给unicode()构造函数(在suds/sax/text.py 的第 43 行调用)。

您必须将来自数据库的字符串转换为unicode对象;例如,如果您的数据库以 UTF-8 编码:

title = product[1].decode("UTF-8")

有关详细信息,请参阅str.decode()文档。

于 2010-06-01T22:42:29.700 回答