• python 列表转字典


    def func_data(text):
    
        data = dict()
    
        for kv in text.split(','):
            k_v = kv.split(':')
            data[k_v[0]] = k_v[1]
    
        return data
    import json
    import xml.etree.cElementTree as ET
    from urllib.parse import urlparse
    
    >>> a = '{"msg_content": {"msg_type": "wechat_msg", "data": "{\"msgId\":829,\"content\":\"<msg>\\n<appmsg appid=\\\"\\\" sdkver=\\\"\\\">\\n<des><![CDATA[\u6211\u7ed9\u4f60\u53d1\u4e86\u4e00\u4e2a\u7ea2\u5305\uff0c\u8d76\u7d27\u53bb\u62c6!]]><\\/des>\\n<url><![CDATA[https:\\/\\/wxapp.tenpay.com\\/mmpayhb\\/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\\/url>\\n<type><![CDATA[2001]]><\\/type>\\n<title><![CDATA[\u5fae\u4fe1\u7ea2\u5305]]><\\/title>\\n<thumburl><![CDATA[http:\\/\\/wx.gtimg.com\\/hongbao\\/1701\\/hb.png]]><\\/thumburl>\\n\\n<wcpayinfo>\\n<templateid><![CDATA[7a2a165d31da7fce6dd77e05c300028a]]><\\/templateid>\\n<url><![CDATA[https:\\/\\/wxapp.tenpay.com\\/mmpayhb\\/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\\/url>\\n<iconurl><![CDATA[http:\\/\\/wx.gtimg.com\\/hongbao\\/1701\\/hb.png]]><\\/iconurl>\\n<receivertitle><![CDATA[Best wishes]]><\\/receivertitle>\\n<sendertitle><![CDATA[Best wishes]]><\\/sendertitle>\\n<scenetext><![CDATA[\u5fae\u4fe1\u7ea2\u5305]]><\\/scenetext>\\n<senderdes><![CDATA[\u67e5\u770b\u7ea2\u5305]]><\\/senderdes>\\n<receiverdes><![CDATA[\u9886\u53d6\u7ea2\u5305]]><\\/receiverdes>\\n<nativeurl><![CDATA[wxpay:\\/\\/c2cbizmessagehandler\\/hongbao\\/receivehongbao?msgtype=1&channelid=1&sendid=1000039501201705266007246104036&sendusername=wxid_ar4tit68b6fa22&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\\/nativeurl>\\n<sceneid><![CDATA[1002]]><\\/sceneid>\\n<innertype><![CDATA[0]]><\\/innertype>\\n\\n\\n\\n<scenetext>\u5fae\u4fe1\u7ea2\u5305<\\/scenetext>\\n\\n\\n\\n\\n<\\/wcpayinfo>\\n<\\/appmsg>\\n<fromusername><![CDATA[wxid_ar4tit68b6fa22]]><\\/fromusername>\\n<\\/msg>\",\"createTime\":1495763217000,\"msgSvrId\":6367525297935218796,\"time\":1495763221114,\"isSend\":0,\"wxid\":\"wxid_ar4tit68b6fa22\"}", "mid": 1495763221157}, "device_id": "993654e2"}'
    
    >>> b = json.loads(a)
    >>> b
    {'msg_content': {'msg_type': 'wechat_msg', 'data': '{"msgId":829,"content":"<msg>\n<appmsg appid=\"\" sdkver=\"\">\n<des><![CDATA[我给你发了一个红包,赶紧去拆!]]><\/des>\n<url><![CDATA[https:\/\/wxapp.tenpay.com\/mmpayhb\/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\/url>\n<type><![CDATA[2001]]><\/type>\n<title><![CDATA[微信红包]]><\/title>\n<thumburl><![CDATA[http:\/\/wx.gtimg.com\/hongbao\/1701\/hb.png]]><\/thumburl>\n\n<wcpayinfo>\n<templateid><![CDATA[7a2a165d31da7fce6dd77e05c300028a]]><\/templateid>\n<url><![CDATA[https:\/\/wxapp.tenpay.com\/mmpayhb\/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\/url>\n<iconurl><![CDATA[http:\/\/wx.gtimg.com\/hongbao\/1701\/hb.png]]><\/iconurl>\n<receivertitle><![CDATA[Best wishes]]><\/receivertitle>\n<sendertitle><![CDATA[Best wishes]]><\/sendertitle>\n<scenetext><![CDATA[微信红包]]><\/scenetext>\n<senderdes><![CDATA[查看红包]]><\/senderdes>\n<receiverdes><![CDATA[领取红包]]><\/receiverdes>\n<nativeurl><![CDATA[wxpay:\/\/c2cbizmessagehandler\/hongbao\/receivehongbao?msgtype=1&channelid=1&sendid=1000039501201705266007246104036&sendusername=wxid_ar4tit68b6fa22&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\/nativeurl>\n<sceneid><![CDATA[1002]]><\/sceneid>\n<innertype><![CDATA[0]]><\/innertype>\n\n\n\n<scenetext>微信红包<\/scenetext>\n\n\n\n\n<\/wcpayinfo>\n<\/appmsg>\n<fromusername><![CDATA[wxid_ar4tit68b6fa22]]><\/fromusername>\n<\/msg>","createTime":1495763217000,"msgSvrId":6367525297935218796,"time":1495763221114,"isSend":0,"wxid":"wxid_ar4tit68b6fa22"}', 'mid': 1495763221157}, 'device_id': '993654e2'}
    
    
    >>> data = b.get('msg_content').get('data')
    
    >>> data
    '{"msgId":829,"content":"<msg>\n<appmsg appid=\"\" sdkver=\"\">\n<des><![CDATA[我给你发了一个红包,赶紧去拆!]]><\/des>\n<url><![CDATA[https:\/\/wxapp.tenpay.com\/mmpayhb\/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\/url>\n<type><![CDATA[2001]]><\/type>\n<title><![CDATA[微信红包]]><\/title>\n<thumburl><![CDATA[http:\/\/wx.gtimg.com\/hongbao\/1701\/hb.png]]><\/thumburl>\n\n<wcpayinfo>\n<templateid><![CDATA[7a2a165d31da7fce6dd77e05c300028a]]><\/templateid>\n<url><![CDATA[https:\/\/wxapp.tenpay.com\/mmpayhb\/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\/url>\n<iconurl><![CDATA[http:\/\/wx.gtimg.com\/hongbao\/1701\/hb.png]]><\/iconurl>\n<receivertitle><![CDATA[Best wishes]]><\/receivertitle>\n<sendertitle><![CDATA[Best wishes]]><\/sendertitle>\n<scenetext><![CDATA[微信红包]]><\/scenetext>\n<senderdes><![CDATA[查看红包]]><\/senderdes>\n<receiverdes><![CDATA[领取红包]]><\/receiverdes>\n<nativeurl><![CDATA[wxpay:\/\/c2cbizmessagehandler\/hongbao\/receivehongbao?msgtype=1&channelid=1&sendid=1000039501201705266007246104036&sendusername=wxid_ar4tit68b6fa22&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]><\/nativeurl>\n<sceneid><![CDATA[1002]]><\/sceneid>\n<innertype><![CDATA[0]]><\/innertype>\n\n\n\n<scenetext>微信红包<\/scenetext>\n\n\n\n\n<\/wcpayinfo>\n<\/appmsg>\n<fromusername><![CDATA[wxid_ar4tit68b6fa22]]><\/fromusername>\n<\/msg>","createTime":1495763217000,"msgSvrId":6367525297935218796,"time":1495763221114,"isSend":0,"wxid":"wxid_ar4tit68b6fa22"}'
    >>> _data = json.loads(data)
    >>> _data
    {'content': '<msg>
    <appmsg appid="" sdkver="">
    <des><![CDATA[我给你发了一个红包,赶紧去拆!]]></des>
    <url><![CDATA[https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]></url>
    <type><![CDATA[2001]]></type>
    <title><![CDATA[微信红包]]></title>
    <thumburl><![CDATA[http://wx.gtimg.com/hongbao/1701/hb.png]]></thumburl>
    
    <wcpayinfo>
    <templateid><![CDATA[7a2a165d31da7fce6dd77e05c300028a]]></templateid>
    <url><![CDATA[https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]></url>
    <iconurl><![CDATA[http://wx.gtimg.com/hongbao/1701/hb.png]]></iconurl>
    <receivertitle><![CDATA[Best wishes]]></receivertitle>
    <sendertitle><![CDATA[Best wishes]]></sendertitle>
    <scenetext><![CDATA[微信红包]]></scenetext>
    <senderdes><![CDATA[查看红包]]></senderdes>
    <receiverdes><![CDATA[领取红包]]></receiverdes>
    <nativeurl><![CDATA[wxpay://c2cbizmessagehandler/hongbao/receivehongbao?msgtype=1&channelid=1&sendid=1000039501201705266007246104036&sendusername=wxid_ar4tit68b6fa22&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be]]></nativeurl>
    <sceneid><![CDATA[1002]]></sceneid>
    <innertype><![CDATA[0]]></innertype>
    
    
    
    <scenetext>微信红包</scenetext>
    
    
    
    
    </wcpayinfo>
    </appmsg>
    <fromusername><![CDATA[wxid_ar4tit68b6fa22]]></fromusername>
    </msg>', 'isSend': 0, 'time': 1495763221114, 'wxid': 'wxid_ar4tit68b6fa22', 'msgSvrId': 6367525297935218796, 'createTime': 1495763217000, 'msgId': 829}
    >>> red_packet_content = _data.get('content')
    >>> red_packet_tree = ET.fromstring(red_packet_content.replace('\/', '/'))
    >>> red_url = ''
    >>> 
    >>> for sendertitle in red_packet_tree.iter(tag='sendertitle'):
    ...     print(sendertitle.text)
    ...     remarks = sendertitle.text
    ... 
    Best wishes
    >>> for url in red_packet_tree.iter(tag='url'):
    ...     red_url = url.text
    ... 
    >>> for url in red_packet_tree.iter(tag='url'):
    ...     red_url = url.text
    ...     print(url.text)
    ... 
    https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be
    https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be
    >>> from urllib.parse import urlparse
    >>> 
    >>> _url_value = urlparse(red_url).query
    >>> print(_url_value)
    showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039501201705266007246104036&ver=6&sign=1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be

    >>> url_value = (_url_value.replace('&', ',')).replace('=', ':') >>> print(url_value) showwxpaytitle:1,msgtype:1,channelid:1,sendid:1000039501201705266007246104036,ver:6,sign:1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be >>> def func_data(text): ... data = dict() ... for kv in text.split(','): ... k_v = kv.split(':') ... data[k_v[0]] = k_v[1] ... return data ... >>> sendid = func_data(url_value).get('sendid') >>> print(sendid) 1000039501201705266007246104036 >>> sign = func_data(url_value).get('sign') >>> print(sign) 1e933b4942c56a30fb5f998283b953a421a8219ac9ccc6b3756095ce0a04c727208a41bf836a4ad7068c0777b02a0f4e76501369ae011652138910bbf6218fe947080afee1041c0d39641e779cd3f90dc2665a6ae496aedae1035ac18821d9be >>>
  • 相关阅读:
    解决Spring+Quartz无法自动注入bean问题
    MacBook PRO蓝牙无法搜索设备
    解决HP打印机错误:Couldn't open fifo
    SQL调优
    ALTER SEQUENCE 修改序列解决唯一约束冲突 unique constraint violated
    Linux Unix 环境变量设置实例
    DB2解除锁表
    ExtJS远程数据-本地分页
    安装mysql
    celery使用
  • 原文地址:https://www.cnblogs.com/bevis-blog/p/6908244.html
Copyright © 2020-2023  润新知