Python split函数处理str转list的问题,pythonsplit,str1 = " [[1
Python split函数处理str转list的问题,pythonsplit,str1 = " [[1
str1 = " [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}]"str2 = [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}]
需要使用json函数处理以上字符串。
其中,json函数只能处理str2,str1没法处理。
问题补充:
如果只是简单的是split函数的话,对于如下str就没法正确转换成对应list了
str = """[[[[["http://weibo.com/1682191304/Akwuncb2q","\u5168\u7403\u7ecf\u5178IT\u6570\u7801\u6392\u884c\u699c","【指尖足球传奇(Flick Kick<em> Football</em> Legends)】这是一款体育竞技类的游戏,在这一款游戏中将给你带来不一样的足球体验,你将在游戏中参与团队的建设与管理,以及多层次的联赛。游戏的画面是一个充满活力的漫画风格,洋溢着生动的人物,漂亮的场景和丰富的动画,你将指导你的团队朝着冠军出击。",[["http://ww3.sinaimg.cn/thumbnail/644433c8jw1eayh5k3znej208d0dadhb.jpg","http://ww3.sinaimg.cn/bmiddle/644433c8jw1eayh5k3znej208d0dadhb.jpg","http://ww3.sinaimg.cn/large/644433c8jw1eayh5k3znej208d0dadhb.jpg"]],"http://weibo.com/1682191304","null","null","null","false","false","null"],["","http://tp1.sinaimg.cn/1682191304/50/5674287539/0",0,"null","",""],1385452475,"http://weibo.com","null","null","null","4267537757014820535",["3648914512902370",1,"-7203662616354568164"]],6,0],[[["http://weibo.com/1723999295/Akw628T4M","EASTTOUCH","Nike公司在里約熱內盧發佈了全新巴西國家隊主場球衣! 球衣將12月5日起於香港Nike<em> Football</em>(旺角花園街66-68號舖發售。<a href=\"http://instagram.com/p/hKyGhWmEW9/\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '-4178570757491017010')\" onmousedown=\"sclick(this, 'http://instagram.com/p/hKyGhWmEW9/', "1,mb,5950525529909388969,1", 26)\"> http://t.cn/8kLtjXj</a>\n",[["http://ww4.sinaimg.cn/thumbnail/66c2243fjw1eayff64yeyj20hs0hsdgt.jpg","http://ww4.sinaimg.cn/bmiddle/66c2243fjw1eayff64yeyj20hs0hsdgt.jpg","http://ww4.sinaimg.cn/large/66c2243fjw1eayff64yeyj20hs0hsdgt.jpg"]],"http://weibo.com/1723999295","null","null","null","false","true",0],["","http://tp4.sinaimg.cn/1723999295/50/1281953776/1",0,"null","",""],1385448878,"http://weibo.com","null","null","null","-4425967400580556024",["3648899422118340",1,"7560513760007101700"]],6,1],[[["http://weibo.com/1776709857/Akw05BGXc","\u82f1\u8bed\u8f85\u5bfc\u9891\u9053","【英语这样说】Interest/1.I'm interested in cooking.我对烹饪感兴趣。2.I'm intrested in it.我对这很感兴趣。3.I find no interest in it.我对这个不感兴趣。4.Are you interested in it?你对这个感兴趣吗?5.<em>Football</em> doesn't interest me at all.足球一点也提不起我的兴趣。",[["http://ww4.sinaimg.cn/thumbnail/69e670e1gw1eayezwowksj20dw0990tb.jpg","http://ww4.sinaimg.cn/bmiddle/69e670e1gw1eayezwowksj20dw0990tb.jpg","http://ww4.sinaimg.cn/large/69e670e1gw1eayezwowksj20dw0990tb.jpg"]],"http://weibo.com/1776709857","null","null","null","false","true",0],["","http://tp2.sinaimg.cn/1776709857/50/1293701149/0",0,"null","",""],1385448002,"http://weibo.com","null","null","null","8972148830071385391",["3648895738983254",1,"-7527736381955494610"]],6,2],[[["http://weibo.com/2018613985/Akwuu0dcg","\u5468\u7d2b\u535a_\u5c0f\u5730\u6f0f\u6392\u5b50","【视频:屌炸!韩国女梅西展示逆天盘带KOREA_girl_freestyle_<em>football</em>】 (分享自<a href=\"http://weibo.com/n/%E4%BC%98%E9%85%B7%E7%BD%91\" target=\"_blank\" mt=\"url\" onmousedown=\"sclick(this, 'http://weibo.com/n/优酷网', "3,mb,5950525529909388969,3", 24)\"> @优酷网</a>)<a href=\"http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '6129194469497588177')\" onmousedown=\"sclick(this, 'http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html', "3,mb,5950525529909388969,3", 26)\"> http://t.cn/8k22nob</a>\n",[["http://ww4.sinaimg.cn/thumbnail/78519ae1jw1eayh5uhdlbj203k02ot8k.jpg","http://ww4.sinaimg.cn/bmiddle/78519ae1jw1eayh5uhdlbj203k02ot8k.jpg","http://ww4.sinaimg.cn/large/78519ae1jw1eayh5uhdlbj203k02ot8k.jpg"]],"http://weibo.com/2018613985","null","null","null","false","true",0],["","http://tp2.sinaimg.cn/2018613985/50/5672235690/1",0,"null","",""],1385452491,"http://weibo.com","null","null","null","372999694477953129",["3648914580050732",1,"414138532007079659"]],6,3],[[["http://weibo.com/2871825574/AkwwBueCD","--\u807d\u98a8","一年一度的<em>Football</em> Extravaganza Awards颁奖典礼近日在伦敦格罗夫纳豪斯酒店(Grosvenor House)盛大举行。",[["http://ww2.sinaimg.cn/thumbnail/ab2c94a6gw1eayhba2o24j20fa0aamxn.jpg","http://ww2.sinaimg.cn/bmiddle/ab2c94a6gw1eayhba2o24j20fa0aamxn.jpg","http://ww2.sinaimg.cn/large/ab2c94a6gw1eayhba2o24j20fa0aamxn.jpg"]],"http://weibo.com/2871825574","null","null","null","false","false","null"],["","http://tp3.sinaimg.cn/2871825574/50/5637390832/1",0,"null","",""],1385452807,"http://weibo.com","null","null","null","1272101906115654551",["3648915897206051",1,"3674992790126310437"]],6,4],[[["http://weibo.com/3224670990/AkuRmvC5r","JD\u4eac\u4e1c-\u4e2d\u8d85","【前韩国国青队长收申花报价 金英权队友曾零封国足】据《<em>football</em>-station》援引日本媒体《日刊体育》报道,现效力于日本J1联赛球队FC东京的韩国后卫张贤秀收到来自申花的报价,下赛季有可能征战中超。他不仅和金英权一同入选过韩国国字号球队,还在今年东亚杯韩国队与中国队的比赛中首发出场。",[["http://ww2.sinaimg.cn/thumbnail/c034930ejw1eay9ylrrc7j20fa05l0t4.jpg","http://ww2.sinaimg.cn/bmiddle/c034930ejw1eay9ylrrc7j20fa05l0t4.jpg","http://ww2.sinaimg.cn/large/c034930ejw1eay9ylrrc7j20fa05l0t4.jpg"]],"http://weibo.com/3224670990","null","null","null","false","true",0],["","http://tp3.sinaimg.cn/3224670990/50/40022875522/0",0,"null","",""],1385437545,"http://weibo.com","null","null","null","-5704329473027886849",["3648851887534577",1,"-5772202263678220272"]],6,5],[[["http://weibo.com/1393508717/AkwAJ8dtg","\u85e4\u7f20\u697c","已收藏,推荐大家观看!【视频:屌炸!韩国女梅西展示逆天盘带KOREA_girl_freestyle_<em>football</em>】<a href=\"http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '6129194469497588177')\" onmousedown=\"sclick(this, 'http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html', "6,mb,5950525529909388969,6", 26)\">http://t.cn/8k22nob</a>(来<a href=\"http://weibo.com/n/%E4%BC%98%E9%85%B7%E7%BD%91\" target=\"_blank\" mt=\"url\" onmousedown=\"sclick(this, 'http://weibo.com/n/优酷网', "6,mb,5950525529909388969,6", 24)\"> @优酷网</a> 看我更多收藏视频:<a href=\"http://i.youku.com/u/UMTU5NjQzMTI=\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '866153775724650286')\" onmousedown=\"sclick(this, 'http://i.youku.com/u/UMTU5NjQzMTI=', "6,mb,5950525529909388969,6", 26)\">http://t.cn/zO6tWS5</a>)","null","http://weibo.com/1393508717","null","null","null","false","false","null"],["","http://tp2.sinaimg.cn/1393508717/50/1291368718/1",0,"null","",""],1385453415,"http://weibo.com","null","null","null","-1832470777861108623",["3648918451958410",1,"-2866251490025652156"]],6,6],[[["http://weibo.com/2315285143/AkwmlzDJL","\u679c\u6838_Nutlet","【视频:屌炸!韩国女梅西展示逆天盘带KOREA_girl_freestyle_<em>football</em>】 (分享自<a href=\"http://weibo.com/n/%E4%BC%98%E9%85%B7%E7%BD%91\" target=\"_blank\" mt=\"url\" onmousedown=\"sclick(this, 'http://weibo.com/n/优酷网', "7,mb,5950525529909388969,7", 24)\"> @优酷网</a>) 看完这个我表示对自己的脚踝捉急。<a href=\"http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '6129194469497588177')\" onmousedown=\"sclick(this, 'http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html', "7,mb,5950525529909388969,7", 26)\"> http://t.cn/8k22nob</a>\n",[["http://ww1.sinaimg.cn/thumbnail/8a007297jw1eaygkzr7bmj203k02ot8k.jpg","http://ww1.sinaimg.cn/bmiddle/8a007297jw1eaygkzr7bmj203k02ot8k.jpg","http://ww1.sinaimg.cn/large/8a007297jw1eaygkzr7bmj203k02ot8k.jpg"]],"http://weibo.com/2315285143","null","null","null","false","false","null"],["","http://tp4.sinaimg.cn/2315285143/50/5629089767/1",0,"null","",""],1385451290,"http://weibo.com","null","null","null","-4021532266690167299",["3648909538494233",1,"-6444754462960392468"]],6,7],[[["http://weibo.com/1742067771/AkwiatSnG","Eiffel--tour--LA","\n<a href=\"http://weibo.com/n/Lu%E5%93%A5%E8%A6%81%E5%81%9A%E9%9D%A2%E5%8C%85%E7%94%B7\" target=\"_blank\" mt=\"url\" onmousedown=\"sclick(this, 'http://weibo.com/n/Lu哥要做面包男', "8,mb,5950525529909388969,8", 24)\">@Lu哥要做面包男</a> 我等屌丝完败 【视频:屌炸!韩国女梅西展示逆天盘带KOREA_girl_freestyle_<em>football</em>】 (分享自<a href=\"http://weibo.com/n/%E4%BC%98%E9%85%B7%E7%BD%91\" target=\"_blank\" mt=\"url\" onmousedown=\"sclick(this, 'http://weibo.com/n/优酷网', "8,mb,5950525529909388969,8", 24)\"> @优酷网</a>)<a href=\"http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '6129194469497588177')\" onmousedown=\"sclick(this, 'http://v.youku.com/v_show/id_XNjM1NDExOTQ0.html', "8,mb,5950525529909388969,8", 26)\"> http://t.cn/8k22nob</a>\n",[["http://ww4.sinaimg.cn/thumbnail/67d5d83bjw1eayga83hcij203k02ot8k.jpg","http://ww4.sinaimg.cn/bmiddle/67d5d83bjw1eayga83hcij203k02ot8k.jpg","http://ww4.sinaimg.cn/large/67d5d83bjw1eayga83hcij203k02ot8k.jpg"]],"http://weibo.com/1742067771","null","null","null","false","false","null"],["","http://tp4.sinaimg.cn/1742067771/50/40039638320/1",0,"null","",""],1385450671,"http://weibo.com","null","null","null","-8762854005281591336",["3648906947120556",1,"1463452194760331847"]],6,8],[[["http://weibo.com/1550915063/Akwy9ecDD","\u897f\u96c5\u56fe\u592a\u9633\u96e8","发表了博文 《28 More Rules of Thankagiving<em> Football</em>》 - This is the third-annual edition of the Journal's<a href=\"http://blog.sina.com.cn/s/blog_5c7115f70101ax15.html\" target=\"_blank\" mt=\"url\" onmouseover=\"if(attachShortLink)attachShortLink(this, '6221082141177607678')\" onmousedown=\"sclick(this, 'http://blog.sina.com.cn/s/blog_5c7115f70101ax15.html', "9,mb,5950525529909388969,9", 26)\"> http://t.cn/8kLIsg8</a>\n","null","http://weibo.com/1550915063","null","null","null","false","false","null"],["","http://tp4.sinaimg.cn/1550915063/50/1298550509/0",0,"null","",""],1385453032,"http://weibo.com","null","null","null","-47280175777735748",["3648916853385177",1,"-2492759631663434484"]],6,9]],[],31900,{"mb":177,"u":100},"false","null",["null","null","null"],[],"5950525529909388969",[],[],"null",[],"null"]"""
str3 = eval(str1)print str3 == str2
首先,str2不是个字符串。
str1这种情况可以通过2种方式来操作:
1. 使用json库
代码如下:
import jsonstr1 = " [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}]"str1_standard = str1.replace('(','[').replace(')',']').replace('\'','"')str1_json = json.loads(str1_standard)
这么做是因为python在dump json中会把list输出为tuple,同时python支持'来标定字符串,所以需要将这种模式调整过来就行了。
可以参考这个链接:点我
2. 使用eval
代码如下:
str1 = " [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}]"str1_evaled = eval(str1)
同样也可以方便取得数据,不过这种方法对数据要求比较严格,在真实使用环境中最好加上try,except,免得直接抛出异常后停止运行。
eval相关的资料可以去google一下。
3. 补充问题
一般而言,python下 可以使用 string 的 split 方法将字符串拆成列表,例如:
"a, b, c".split(',')
结果是
['a', ' b', ' c']
编橙之家文章,
相关内容
- python归替RuntimeError是什么错误?,pythonruntimeerror,class S
- Python中使用fork是否适合?,Python使用fork,通常在java开发
- win系统安装scipy失败问题,提示没有BLAS的原因是什么?
- 求教python数组、序列与迭代综合应用问题,python数组
- python 3版本程序在调用shell 指令怎么样才能获得返回值
- psycopg2使用遇到InternalError错误是什么原因,,执行完第
- 在python里面怎么样使用socket连接,pythonsocket连接,尝试连
- python中证明x的平方加上y的平方等于3,没有有理数解?
- pycharm运行scrapy除了cmd外还有别的吗,pycharmscrapy,目前启
- 使用Python解析豆瓣网API的源码解析,pythonapi,为何我在浏
评论关闭