首页 > Python:str转list

Python:str转list

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","&#12304;&#25351;&#23574;&#36275;&#29699;&#20256;&#22855;&#65288;Flick Kick<em> Football</em> Legends&#65289;&#12305;&#36825;&#26159;&#19968;&#27454;&#20307;&#32946;&#31454;&#25216;&#31867;&#30340;&#28216;&#25103;&#65292;&#22312;&#36825;&#19968;&#27454;&#28216;&#25103;&#20013;&#23558;&#32473;&#20320;&#24102;&#26469;&#19981;&#19968;&#26679;&#30340;&#36275;&#29699;&#20307;&#39564;&#65292;&#20320;&#23558;&#22312;&#28216;&#25103;&#20013;&#21442;&#19982;&#22242;&#38431;&#30340;&#24314;&#35774;&#19982;&#31649;&#29702;&#65292;&#20197;&#21450;&#22810;&#23618;&#27425;&#30340;&#32852;&#36187;&#12290;&#28216;&#25103;&#30340;&#30011;&#38754;&#26159;&#19968;&#20010;&#20805;&#28385;&#27963;&#21147;&#30340;&#28459;&#30011;&#39118;&#26684;&#65292;&#27915;&#28322;&#30528;&#29983;&#21160;&#30340;&#20154;&#29289;&#65292;&#28418;&#20142;&#30340;&#22330;&#26223;&#21644;&#20016;&#23500;&#30340;&#21160;&#30011;&#65292;&#20320;&#23558;&#25351;&#23548;&#20320;&#30340;&#22242;&#38431;&#26397;&#30528;&#20896;&#20891;&#20986;&#20987;&#12290;",[["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&#20844;&#21496;&#22312;&#37324;&#32004;&#29105;&#20839;&#30439;&#30332;&#20296;&#20102;&#20840;&#26032;&#24052;&#35199;&#22283;&#23478;&#38538;&#20027;&#22580;&#29699;&#34915;&#65281; &#29699;&#34915;&#23559;12&#26376;5&#26085;&#36215;&#26044;&#39321;&#28207;Nike<em> Football</em>&#65288;&#26106;&#35282;&#33457;&#22290;&#34903;66-68&#34399;&#33302;&#30332;&#21806;&#12290;<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/', &quot;1,mb,5950525529909388969,1&quot;, 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","&#12304;&#33521;&#35821;&#36825;&#26679;&#35828;&#12305;Interest/1.I'm interested in cooking.&#25105;&#23545;&#28921;&#39274;&#24863;&#20852;&#36259;&#12290;2.I'm intrested in it.&#25105;&#23545;&#36825;&#24456;&#24863;&#20852;&#36259;&#12290;3.I find no interest in it.&#25105;&#23545;&#36825;&#20010;&#19981;&#24863;&#20852;&#36259;&#12290;4.Are you interested in it?&#20320;&#23545;&#36825;&#20010;&#24863;&#20852;&#36259;&#21527;&#65311;5.<em>Football</em> doesn't interest me at all.&#36275;&#29699;&#19968;&#28857;&#20063;&#25552;&#19981;&#36215;&#25105;&#30340;&#20852;&#36259;&#12290;",[["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","&#12304;&#35270;&#39057;&#65306;&#23628;&#28856;&#65281;&#38889;&#22269;&#22899;&#26757;&#35199;&#23637;&#31034;&#36870;&#22825;&#30424;&#24102;KOREA_girl_freestyle_<em>football</em>&#12305; &#65288;&#20998;&#20139;&#33258;<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/&#20248;&#37239;&#32593;', &quot;3,mb,5950525529909388969,3&quot;, 24)\"> @&#20248;&#37239;&#32593;</a>&#65289;<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', &quot;3,mb,5950525529909388969,3&quot;, 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","&#19968;&#24180;&#19968;&#24230;&#30340;<em>Football</em> Extravaganza Awards&#39041;&#22870;&#20856;&#31036;&#36817;&#26085;&#22312;&#20262;&#25958;&#26684;&#32599;&#22827;&#32435;&#35946;&#26031;&#37202;&#24215;&#65288;Grosvenor House&#65289;&#30427;&#22823;&#20030;&#34892;&#12290;",[["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","&#12304;&#21069;&#38889;&#22269;&#22269;&#38738;&#38431;&#38271;&#25910;&#30003;&#33457;&#25253;&#20215; &#37329;&#33521;&#26435;&#38431;&#21451;&#26366;&#38646;&#23553;&#22269;&#36275;&#12305;&#25454;&#12298;<em>football</em>-station&#12299;&#25588;&#24341;&#26085;&#26412;&#23186;&#20307;&#12298;&#26085;&#21002;&#20307;&#32946;&#12299;&#25253;&#36947;&#65292;&#29616;&#25928;&#21147;&#20110;&#26085;&#26412;J1&#32852;&#36187;&#29699;&#38431;FC&#19996;&#20140;&#30340;&#38889;&#22269;&#21518;&#21355;&#24352;&#36132;&#31168;&#25910;&#21040;&#26469;&#33258;&#30003;&#33457;&#30340;&#25253;&#20215;&#65292;&#19979;&#36187;&#23395;&#26377;&#21487;&#33021;&#24449;&#25112;&#20013;&#36229;&#12290;&#20182;&#19981;&#20165;&#21644;&#37329;&#33521;&#26435;&#19968;&#21516;&#20837;&#36873;&#36807;&#38889;&#22269;&#22269;&#23383;&#21495;&#29699;&#38431;&#65292;&#36824;&#22312;&#20170;&#24180;&#19996;&#20122;&#26479;&#38889;&#22269;&#38431;&#19982;&#20013;&#22269;&#38431;&#30340;&#27604;&#36187;&#20013;&#39318;&#21457;&#20986;&#22330;&#12290;",[["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","&#24050;&#25910;&#34255;&#65292;&#25512;&#33616;&#22823;&#23478;&#35266;&#30475;&#65281;&#12304;&#35270;&#39057;&#65306;&#23628;&#28856;&#65281;&#38889;&#22269;&#22899;&#26757;&#35199;&#23637;&#31034;&#36870;&#22825;&#30424;&#24102;KOREA_girl_freestyle_<em>football</em>&#12305;<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', &quot;6,mb,5950525529909388969,6&quot;, 26)\">http://t.cn/8k22nob</a>&#65288;&#26469;<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/&#20248;&#37239;&#32593;', &quot;6,mb,5950525529909388969,6&quot;, 24)\"> @&#20248;&#37239;&#32593;</a> &#30475;&#25105;&#26356;&#22810;&#25910;&#34255;&#35270;&#39057;&#65306;<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=', &quot;6,mb,5950525529909388969,6&quot;, 26)\">http://t.cn/zO6tWS5</a>&#65289;","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","&#12304;&#35270;&#39057;&#65306;&#23628;&#28856;&#65281;&#38889;&#22269;&#22899;&#26757;&#35199;&#23637;&#31034;&#36870;&#22825;&#30424;&#24102;KOREA_girl_freestyle_<em>football</em>&#12305; &#65288;&#20998;&#20139;&#33258;<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/&#20248;&#37239;&#32593;', &quot;7,mb,5950525529909388969,7&quot;, 24)\"> @&#20248;&#37239;&#32593;</a>&#65289; &#30475;&#23436;&#36825;&#20010;&#25105;&#34920;&#31034;&#23545;&#33258;&#24049;&#30340;&#33050;&#36381;&#25417;&#24613;&#12290;<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', &quot;7,mb,5950525529909388969,7&quot;, 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&#21733;&#35201;&#20570;&#38754;&#21253;&#30007;', &quot;8,mb,5950525529909388969,8&quot;, 24)\">@Lu&#21733;&#35201;&#20570;&#38754;&#21253;&#30007;</a> &#25105;&#31561;&#23628;&#19997;&#23436;&#36133; &#12304;&#35270;&#39057;&#65306;&#23628;&#28856;&#65281;&#38889;&#22269;&#22899;&#26757;&#35199;&#23637;&#31034;&#36870;&#22825;&#30424;&#24102;KOREA_girl_freestyle_<em>football</em>&#12305; &#65288;&#20998;&#20139;&#33258;<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/&#20248;&#37239;&#32593;', &quot;8,mb,5950525529909388969,8&quot;, 24)\"> @&#20248;&#37239;&#32593;</a>&#65289;<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', &quot;8,mb,5950525529909388969,8&quot;, 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","&#21457;&#34920;&#20102;&#21338;&#25991; &#12298;28 More Rules of Thankagiving<em> Football</em>&#12299; - 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', &quot;9,mb,5950525529909388969,9&quot;, 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"]
"""

首先,str2不是个字符串。
str1这种情况可以通过2种方式来操作:

1. 使用json库

代码如下:

import json
str1 = " [[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. 补充问题

这个不能通过json格式化的原因是你的str中包含\"这种情况。
我将补充问题中的str简化一下,变为:
str = """["a","b","<a href=\"xxxx\">"]""",这种形式其实是等效于
str = """["a","b","<a href="xxxx">"]"""
那么这个字符串中就会出现xxxx这一段什么都不属于,所以应该改成:
str = """["a","b","<a href=\\"xxxx\\">"]"""
json.loads(str)就可以了。


一般而言,python下 可以使用 string 的 split 方法将字符串拆成列表,例如:

"a, b, c".split(',')

结果是

['a', ' b', ' c']

str3 = eval(str1)
print str3 == str2
【热门文章】
【热门文章】