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>\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 列表转字典的更多相关文章
- Python 列表生成式 & 字典生成式
Python 列表生成式 & 字典生成式 通过生成式可以更加简洁地生成列表和字典 列表生成式 对比 直接生成数据后加入列表示例: user_list = list() for i in ran ...
- Python列表,元组,字典,序列,引用
1.列表 # Filename: using_list.py # This is my shopping list shoplist=["apple", "mango&q ...
- python 列表,字典,元组,字符串,常用函数
飞机票 一.列表方法 1.ls.extend(object) 向列表ls中插入object中的每个元素,object可以是字符串,元组和列表(字符串“abc”中包含3个元组),相当于ls和object ...
- 【277】◀▶ Python 列表/元组/字典说明
目录: 前言 一.访问列表中的值 二.更新列表 三.删除列表元素 四.Python 列表脚本操作符 五.Python 列表函数 & 方法 参考:Python 列表(List)使用说明 列表截取 ...
- python列表和字典的迭代
1.列表和字典的迭代 程序开发中,对列表和字典进行迭代是非常常见的事情. 字典一般可以选择对key进行迭代.对value迭代和对key/value一起迭代 >>> d = {'a': ...
- Python 列表,元组,字典
0)字符串切片 py_str = 'python' >>>py_str[0] #取第一个字符串,返回值为"p",超出范围会报错 >>>py_st ...
- Python 列表/元组/字典总结
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...
- Python列表,元组,字典,字符串方法笔记
01. 列表 1.1 列表的定义 List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组 专门用于存储 一串 信息 列表用 [] 定义,数据 之间使用 , 分隔 列 ...
- python 列表和字典的引用与复制(copy)
列表或字典的引用: 引用针对变量的时候,传递引用后,对引用后的对象的值进行改变是不会影响到原值的:而列表不一样如: spam =42 cheese = spam spam =100 print(spa ...
- python 列表转为字典的两个小方法
1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...
随机推荐
- ios UIImagePickerController简单说明
首先,VC中添加#import <MobileCoreServices/MobileCoreServices.h> 使用(NSString *) kUTTypeImage定义在其中 判断是 ...
- ios 网络/本地播放器
推荐播放器: LRLAVPlayer相对易懂好修改,调整添加内容. https://github.com/codeWorm2015/videoPlayer NSString*path=[[NSBund ...
- 关于Git增、删、改源地址问题
在上篇博客中我们了解了Git的基本使用,如果你已经建立了一个远程代码库,并且遇到了远程代码库源地址修改的问题,那么这篇博客可能会帮到你. 1.如何查看当前远程Git库源地址呢? $git remote ...
- 如何快速理解JavaScript 中重要语句for循环
一.基本结构:for(起始状态:判断条件:状态改变){ 执行语句: } 执行顺序:for(var i=1;i<3;i++){ alert(i); } 1.判断条件 2.执行语句 3. ...
- mybatisPeizhixml文件的层次结构
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC & ...
- Mobile game analysis
Let's take a look at a very popular mobile game "Garena 传说对决" . It would be very interesti ...
- Angular2.js——多个组件
目前我们的英雄列表和英雄详情位于同一个文件的同一个组件中,我们将来可能会受到新的需求,修改这个组件又不能影响另外一个.然而每一次更改都会给这两个组件带来风险和双倍的测试负担,没有任何好处.如果我们需要 ...
- 北漂面试经历(一(两)年工作经验)-- Java基础部分
Java基础部分 常量和变量的区别:final 关键词修饰的变量是恒定不变的,如果还有static关键词修饰的话,常常称为编译期常量.变量,运行时可以修改其引用. Java基本类似有哪些 1 Byte ...
- bootstrap 获得轮播中的索引或当前活动的焦点对象
今天用bootstrap做一个轮播,当轮播滚到每张图的时候,在页面下面就显示相对应的内容,那么问题来了:为了轮播图的可扩展性,我们肯定需要知道当前活动(显示图片)的索引号,查了bootstrap文档, ...
- MVC两种获取上传的文件数据变量的方式
第一种方式,在控制器中利用HttpPostedFileBase参数对象获取. [HttpPost] public ActionResult SaveFile(HttpPostedFileBase up ...