Skip to main content
 首页 » 编程设计

python suds——WSDL 包含带有重音符号的枚举值

2025年05月04日54zdz8207

在我遇到这个 WSDL 之前,我在使用 suds 之前从来没有遇到过任何问题。 (这只是导致问题的部分)

<s:simpleType name="ProductFormat"> 
   <s:restriction base="s:string"> 
      <s:enumeration value="Papier"/> 
      <s:enumeration value="Numérique"/> 
      <s:enumeration value="PapierEtNumérique"/> 
   </s:restriction> 
</s:simpleType> 

如您所见,当我尝试创建值时,值中有重音符号,并且肥皂水嗡嗡作响:

product_format = self.client.factory.create('ProductFormat') 

这是回溯的结尾:

File "/home/andre/Documents/archambault/apps/onix/management/commands/import_sogides_onix.py", line 58, in get_catalog 
    product_format = self.client.factory.create('ProductFormat') 
  File "/home/andre/Documents/archambault/envs/lib/python2.6/site-packages/suds/client.py", line 240, in create 
    setattr(result, e.name, e.name) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128) 

看起来 Suds 不喜欢 Numérique 的“é”(位置 3)。据您所知,有没有办法避免编辑 client.py ?

谢谢

请您参考如下方法:

suds 接口(interface)将这些枚举转换为对象属性,而 Python 2.* 不允许标识符名称使用 unicode 字形。在 Python 3.2 中这有效:

>>> class A(object): 
        pass 
 
>>> a = A() 
>>> setattr(a, 'tést', 1) 
>>> a.tést 
1 

在 Python 2.6 中,这会引发 UnicodeEncodeError:

>>> setattr(a, u'tést', 1) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128) 

为 Python 2.* 修复此问题似乎需要大量工作:

  • 检查是否 e.name != unidecode(e.name),如果是,将标识符转换为 ascii 使用 取消编码并跟踪原始名称。
  • 在生成 soap 消息时转换回 unicode 版本

我从未在 Python 3.* 中尝试过 suds,但它支持标识符名称中的 unicode 字母。

我的母语是葡萄牙语,在这个 WSDL 中有很多像法语这样的重音字符。即使 Python 3.* 允许这样做,我认为没有令人信服的理由使用英语标识符以外的任何东西——这是一个愚蠢的想法,界面作者是在自找麻烦。 SOAP 背后的整个理念是互操作性,为什么要使用并非所有编程语言都很好支持的功能?