V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jul 10, 2020 · 2595 views
    This topic created in 2127 days ago, the information mentioned may be changed or developed.

    现有 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

    有啥好的办法吗?

    Supplement 1  ·  Jul 10, 2020
    [解决了]
    抱歉可能我表述不太清楚,再简单分享下。
    [诉求]
    现有 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  
       Jul 10, 2020
    dict 的 key 啥时候能重复了,你这是犯傻了还是想要逆天无视数据结构规则了
    sikariba
        2
    sikariba  
       Jul 10, 2020
    dict 的 key 是 hash 的,不能重复,你只能从 dict 序列化到 query string 的这一部分想办法
    cassidyhere
        3
    cassidyhere  
       Jul 10, 2020
    你可以用 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  
       Jul 10, 2020
    为何要 coupon_fee=10&coupon_fee=100&fee_type=CNY ? 这种两个重复的 key 看着不另类吗?
    fonlan
        5
    fonlan  
       Jul 10, 2020
    或者你就手动写解析,转成{'fee_type': 'CNY', 'coupon_fee': ['10', '100']}这样,既然是同名的 key 不如把值放入一个 list
    bnm965321
        6
    bnm965321  
       Jul 10, 2020
    @lake325 这种格式是 query string 规范,是正常的。但是可以用 urllib 直接从上面的 dict 转换
    maobukui
        7
    maobukui  
    OP
       Jul 10, 2020
    [解决了]
    抱歉可能我表述不太清楚,再简单分享下。
    [诉求]
    现有 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
       Jul 10, 2020
    @sikariba 感谢
    maobukui
        9
    maobukui  
    OP
       Jul 10, 2020
    @lake325 哈哈,是的!详见微信支付签名 MD5 校验,就是这样
    wdf86
        10
    wdf86  
       Jul 10, 2020
    xmltodcit 这个库可以了解下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3394 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 12:34 · PVG 20:34 · LAX 05:34 · JFK 08:34
    ♥ Do have faith in what you're doing.