V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
maobukui
V2EX  ›  Python

请教 Python 中 xml 转 dict 格式--不懂就问

  •  
  •   maobukui · 2020-07-10 09:27:14 +08:00 · 1706 次点击
    这是一个创建于 1357 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现有 a 变量,

    a = """ <xml> <fee_type></fee_type> <coupon_fee></coupon_fee> <coupon_fee></coupon_fee> </xml> """

    因为“coupoen_fee”重复了,通过 xmltodict 转的 dict 格式为 {'fee_type': 'CNY', 'coupon_fee': ['10', '100']}

    期待的格式是 {'fee_type': 'CNY', 'coupon_fee': '10', 'coupon_fee': '100'}

    目的是得到 coupon_fee=10&coupon_fee=100&fee_type=CNY

    有啥好的办法吗?

    第 1 条附言  ·  2020-07-10 10:38:55 +08:00
    [解决了]
    抱歉可能我表述不太清楚,再简单分享下。
    [诉求]
    现有 xml 字符串 a = """ <xml> <fee_type></fee_type> <coupon_fee></coupon_fee> <coupon_fee></coupon_fee> </xml> """
    期望得到 coupon_fee=10&coupon_fee=100&fee_type=CNY
    [实现方式]
    仍然 xml → dict (此时值有列表)
    通过 for 循环判定值为列表情况,便利后,通过 key + "= " + value 拼接了字符串。
    感谢。
    sujin190
        1
    sujin190  
       2020-07-10 09:37:47 +08:00
    dict 的 key 啥时候能重复了,你这是犯傻了还是想要逆天无视数据结构规则了
    sikariba
        2
    sikariba  
       2020-07-10 09:47:39 +08:00
    dict 的 key 是 hash 的,不能重复,你只能从 dict 序列化到 query string 的这一部分想办法
    cassidyhere
        3
    cassidyhere  
       2020-07-10 09:52:05 +08:00
    你可以用 collections.ChainMap,或者参考 flask 里的 MultiDict:
    >>> d = MultiDict([('a', 'b'), ('a', 'c')])
    >>> d
    MultiDict([('a', 'b'), ('a', 'c')])
    >>> d['a']
    'b'
    >>> d.getlist('a')
    ['b', 'c']
    lake325
        4
    lake325  
       2020-07-10 09:53:24 +08:00
    为何要 coupon_fee=10&coupon_fee=100&fee_type=CNY ? 这种两个重复的 key 看着不另类吗?
    fonlan
        5
    fonlan  
       2020-07-10 09:55:50 +08:00
    或者你就手动写解析,转成{'fee_type': 'CNY', 'coupon_fee': ['10', '100']}这样,既然是同名的 key 不如把值放入一个 list
    bnm965321
        6
    bnm965321  
       2020-07-10 10:34:43 +08:00
    @lake325 这种格式是 query string 规范,是正常的。但是可以用 urllib 直接从上面的 dict 转换
    maobukui
        7
    maobukui  
    OP
       2020-07-10 10:37:24 +08:00
    [解决了]
    抱歉可能我表述不太清楚,再简单分享下。
    [诉求]
    现有 xml 字符串 a = """ <xml> <fee_type></fee_type> <coupon_fee></coupon_fee> <coupon_fee></coupon_fee> </xml> """
    期望得到 coupon_fee=10&coupon_fee=100&fee_type=CNY
    [实现方式]
    仍然 xml → dict (此时值有列表)
    通过 for 循环判定值为列表情况,便利后,通过 key + "= " + value 拼接了字符串。
    感谢。
    maobukui
        8
    maobukui  
    OP
       2020-07-10 10:37:38 +08:00
    @sikariba 感谢
    maobukui
        9
    maobukui  
    OP
       2020-07-10 10:38:30 +08:00
    @lake325 哈哈,是的!详见微信支付签名 MD5 校验,就是这样
    wdf86
        10
    wdf86  
       2020-07-10 11:20:36 +08:00
    xmltodcit 这个库可以了解下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5281 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:34 · PVG 17:34 · LAX 02:34 · JFK 05:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.