将unicode转换为Python dict会引发错误

时间:2015-08-10 23:54:29

标签: python regex python-2.7 dictionary unicode

我有Android应用程序从移动应用程序发送短信,如下所示:

my_dict = {\
u'1': u'"sender" : "dz-080008", "text" : "enjoy 10%cashback@amazon.in &15%cashback@amazon app with hdfcbank debit/credit cards on min.purchase of rs5000 12aug-max amount rs1500 *t&c: bit.ly/1isljll", "time" : "1439212386383", "name" : "shyam"', \
u'0': u'"sender" : "am-swiggy", "text" : "please use this otp: 195830", "time" : "1439226026974", "name" : "shyam"', \
u'3': u'"sender" : "md-dgenie", "text" : "your dudegenie code is 6632. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155803426", "name" : "shyam"', \
u'2': u'"sender" : "vx-611112", "text" : "we value your association with us. please call us on 89XXXXXXXX for exclusive offer onlyfor your number 88XXXXXXXX.u can reach us from 10:00 am-06:00 pm", "time" : "1439191024750", "name" : "shyam"',\
}.

但是我无法处理请求并将其存储在我的数据库中,因为应用程序将在单个请求中发送100个sms,并且当我通过提取键值对来处理该请求时,其他请求将会到来。所以为了解决这个问题,我确实将请求存储在每行的数据库中。每行包含一个字符串,如下所示:

<QueryDict: {u'messages': [u'<QueryDict: {u\'10\': [u\'"sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "p"\'], u\'1\': [u\'"sender" : "vm-olacab", "text" : "ola! your total bill for crn111085358 is rs 151. your invoice is on its way to your e-mail shyam.ns114@gmail.com. call us on 080-33553355 for any help.", "time" : "1439319374298", "name" : "p"\'], u\'0\': [u\'"sender" : "ad-hlpcht", "text" : "dear customer, 50% off on premium doorstep laundry. use the coupon code hc50. download the app now (bit.ly/help50chat) or give us a shout at 08039236390.", "time" : "1439356609276", "name" : "p"\'], u\'3\': [u\'"sender" : "vm-olacab", "text" : "say ola to your driver kiran m  (8711045873) for crn111085358. silver tata indica ka 51 a 3940  to pick you up @ 12:06 am, 12aug. please share https://www.olacabs.com/track/939u1pe0m with friends/family and let them track you. call 080-33553355 if you need help.", "time" : "1439318029414", "name" : "p"\'], u\'2\': [u\'"sender" : "vm-olacab", "text" : "ola! your cab has arrived on time. hop on to have an awesome experience with this ride!", "time" : "1439318244463", "name" : "p"\'], u\'5\': [u\'"sender" : "dm-020001", "text" : "get upto 40%off +extra rs150 off@nykaa freedom sale with hdfc bank debit/credit card. code: nykhdfc150 .min purchasers1500. validity 15thaug *t&c bit.ly/1wedi4r", "time" : "1439300971524", "name" : "p"\'], u\'4\': [u\'"sender" : "dm-044210", "text" : "1 week to go!top 400 breeze mobile web transactors to get inr 250* flipkart vouchers/week & ipad mini*t&c apply.start now on.sc.com/breezedownload-in stanchart", "time" : "1439303662943", "name" : "p"\'], u\'7\': [u\'"sender" : "dz-080008", "text" : "enjoy 10%cashback@amazon.in &15%cashback@amazon app with hdfcbank debit/credit cards on min.purchase of rs5000 till 12aug-max amount rs1500 *t&c: bit.ly/1isljll", "time" : "1439212386383", "name" : "p"\'], u\'6\': [u\'"sender" : "am-swiggy", "text" : "please use this otp: 195830", "time" : "1439226026974", "name" : "p"\'], u\'9\': [u\'"sender" : "md-dgenie", "text" : "your dudegenie code is 6632. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155803426", "name" : "p"\'], u\'8\': [u\'"sender" : "vx-611112", "text" : "we value your association with us. please call us on 9739102435for exclusive offer onlyfor your number 889XXXXXXX .u can reach us from 10:00 am-06:00 pm", "time" : "1439191024750", "name" : "p"\']}>']}>

现在解压这种unicode / string的最佳方法是什么。?

2 个答案:

答案 0 :(得分:1)

字符串几乎是字典格式,但不包含在{}中。我想你想要&#39; {&#34;发件人&#34; :&#34; am-swiggy&#34;,...}&#39;

答案 1 :(得分:0)

我错过了什么?你想达到什么目的? jsondem和ast进口的目标是什么?

这不足以满足您的目的吗?

import json

my_dict = {u'1': u'"sender" : "dz-080008", "text" : "enjoy 10%cashback@amazon.in &15%cashback@amazon app with hdfcbank debit/credit cards on min.purchase of rs5000 12aug-max amount rs1500 *t&c: bit.ly/1isljll", "time" : "1439212386383", "name" : "shyam"', u'0': u'"sender" : "am-swiggy", "text" : "please use this otp: 195830", "time" : "1439226026974", "name" : "shyam"', \
u'3': u'"sender" : "md-dgenie", "text" : "your dudegenie code is 6632. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155803426", "name" : "shyam"', \
u'2': u'"sender" : "vx-611112", "text" : "we value your association with us. please call us on 89XXXXXXXX for exclusive offer onlyfor your number 88XXXXXXXX.u can reach us from 10:00 am-06:00 pm", "time" : "1439191024750", "name" : "shyam"',\
}

for i in my_dict.values():
    print json.dumps(i)

**输出**

  

“\”sender \“:\”dz-080008 \“,\”text \“:\”享受10%cashback@amazon.in&amp; 15%cashback @ amazon app with hdfcbank借记卡/信用卡.spurchase of rs5000 12aug-max amount rs1500 * t&amp; c:bit.ly/1isljll \“,\”time \“:\”1439212386383 \“,\”name \“:\”shyam \“”   “发件人”:“dz-080008”,“文字”:“享受10%cashback@amazon.in&amp; 15%现金返还@ amazon应用程序与hdfcbank借记卡/信用卡最低购买额度rs5000 12aug-max金额rs1500 * t&amp; ; c:bit.ly/1isljll“,”time“:”1439212386383“,”name“:”shyam“   “\”sender \“:\”am-swiggy \“,\”text \“:\”请使用此otp:195830 \“,\”time \“:\”1439226026974 \“,\”name \“: \ “希亚姆\””   “发件人”:“am-swiggy”,“text”:“请使用此otp:195830”,“时间”:“1439226026974”,“名称”:“shyam”   “\”sender \“:\”md-dgenie \“,\”text \“:\”你的dudegenie代码是6632.欢迎来到dudegenie的世界!你的愿望,我的命令!\“,\”time \“:\”1439155803426 \“,\”name \“:\”shyam \“”   “发件人”:“md-dgenie”,“text”:“你的dudegenie代码是6632.欢迎来到dudegenie的世界!你的愿望,我的命令!”,“时间”:“1439155803426”,“名字”:“shyam “   “\”sender \“:\”vx-611112 \“,\”text \“:\”我们重视您与我们的关联。请拨打89XXXXXXXX,仅限您的电话号码88XXXXXXXX。我们可以在上午10:00至下午06:00之间联系我们。“”时间\“:\”1439191024750 \“,\”名称\“:\”希亚姆\“”   “发件人”:“vx-611112”,“文字”:“我们非常重视您与我们的联系。请致电我们89XXXXXXXX,仅限您的号码88XXXXXXXX.u可以从上午10:00至下午06:00联系我们” ,“时间”:“1439191024750”,“名称”:“shyam”