【问题标题】:python suds -- WSDL contains enumeration values with accentspython suds -- WSDL 包含带重音符号的枚举值
【发布时间】:2012-03-13 14:18:40
【问题描述】:

在我点击这个 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 ?

谢谢

【问题讨论】:

    标签: python utf-8 suds


    【解决方案1】:

    suds 接口将这些枚举转换为对象属性,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 unidecode 并跟踪原始名称。
    • 生成soap消息时转换回unicode版本

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

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

    【讨论】:

    • 谢谢保罗,你证实了我的恐惧。我在玩 e.name 和 decode(),但是当它工作时,它再也找不到“Numérique”了。我不想把它转换回来,所以我在这里写了..
    • @user112342:很抱歉听到这个消息。你很急吗?或许我可以为您解决这个问题,我可以通过 Internet URL 访问此网络服务吗?
    • 很高兴您提供,但它受密码保护...不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多