请教 Python 中 xml 转 dict 格式--不懂就问 - V2EX
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 2241 次点击
    这是一个创建于 1921 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现有 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     Solana     961 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 19:46 PVG 03:46 LAX 12:46 JFK 15:46
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86