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>\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>', '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
>>>

python 列表转字典的更多相关文章

  1. Python 列表生成式 & 字典生成式

    Python 列表生成式 & 字典生成式 通过生成式可以更加简洁地生成列表和字典 列表生成式 对比 直接生成数据后加入列表示例: user_list = list() for i in ran ...

  2. Python列表,元组,字典,序列,引用

    1.列表 # Filename: using_list.py # This is my shopping list shoplist=["apple", "mango&q ...

  3. python 列表,字典,元组,字符串,常用函数

    飞机票 一.列表方法 1.ls.extend(object) 向列表ls中插入object中的每个元素,object可以是字符串,元组和列表(字符串“abc”中包含3个元组),相当于ls和object ...

  4. 【277】◀▶ Python 列表/元组/字典说明

    目录: 前言 一.访问列表中的值 二.更新列表 三.删除列表元素 四.Python 列表脚本操作符 五.Python 列表函数 & 方法 参考:Python 列表(List)使用说明 列表截取 ...

  5. python列表和字典的迭代

    1.列表和字典的迭代 程序开发中,对列表和字典进行迭代是非常常见的事情. 字典一般可以选择对key进行迭代.对value迭代和对key/value一起迭代 >>> d = {'a': ...

  6. Python 列表,元组,字典

    0)字符串切片 py_str = 'python' >>>py_str[0] #取第一个字符串,返回值为"p",超出范围会报错 >>>py_st ...

  7. Python 列表/元组/字典总结

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...

  8. Python列表,元组,字典,字符串方法笔记

    01. 列表 1.1 列表的定义 List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组 专门用于存储 一串 信息 列表用 [] 定义,数据 之间使用 , 分隔 列 ...

  9. python 列表和字典的引用与复制(copy)

    列表或字典的引用: 引用针对变量的时候,传递引用后,对引用后的对象的值进行改变是不会影响到原值的:而列表不一样如: spam =42 cheese = spam spam =100 print(spa ...

  10. python 列表转为字典的两个小方法

    1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...

随机推荐

  1. MyRocks DDL原理

    最近一个日常实例在做DDL过程中,直接把数据库给干趴下了,问题还是比较严重的,于是赶紧排查问题,撸了下crash堆栈和alert日志,发现是在去除唯一约束的场景下,MyRocks存在一个严重的bug, ...

  2. Mac下安装BeautifulSoup

    1.输入命令su,输入root密码,进入root用户 2.执行命令安装pip: sudo easy_install pip 详细pip介绍:http://blog.csdn.net/olanlanxi ...

  3. css3 新属性

    一 选择器1 兄弟选择器 0 以第一个选择器开始,往后找满足条件的兄弟节点 class~class() <-- lorem+数字 -tab --> 可以输出默认文字2 属性选择器 标签[a ...

  4. 关于制作C语言头文件的思考

    我们接触的第一个c语言代码是这个: #include<stdio.h> main() { printf("Hello World"); } 这是最简单的c代码,然而,它 ...

  5. 利用Python进行简单的图像识别(验证码)

    这是一个最简单的图像识别,将图片加载后直接利用Python的一个识别引擎进行识别 将图片中的数字通过 pytesseract.image_to_string(image)识别后将结果存入到本地的txt ...

  6. percona-xtrabackup安装

    二进制包安装(推荐安装方式,不用安装依赖包,非常方便): 1.下载安二进制包:      wget https://www.percona.com/downloads/XtraBackup/Perco ...

  7. innobackup全备与恢复

    前提:xtrabackup.mysql安装完成,建立测试库reading.测试表test,并插入三条数据. 1.全备:      innobackupex --user=root --password ...

  8. 转:Linux中文显示乱码?如何设置centos显示中文

    怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一般是由于没有安装中文语言包,或者设置的默认语言有问题导 ...

  9. 网际报文控制协议ICMP

    为了更有效地转发IP数据报和提高交付成功的机会,在网际层使用了网际控制报文协议ICMP.ICMP允许主机或路由器报告差错情况和提供有关异常情况的报告.ICMP是因特网的标准协议.但ICMP不是高层协议 ...

  10. 关于WannaCry病毒的见解与预防,我有话说!

    好久没写博客了,自从定性专做技术扩展服务后,已经有两年半没有正式写过博客,要不是这次WannaCry病毒的厉害程度,我也懒得去写博客,为什么呢?写技术文章吗?两年多没有研究新的技术,没有什么好写的!所 ...