0
字符串轉換使用這樣到包含一個python列表,列表
[[1356912000, 13.391120000000, 13.509900000000, 13.391120000000, 13.509320000000, 41.088424560000, 555.033691727713, 13.508273867187],
[1356912900, 13.509320000000, 13.549990000000, 13.424280000000, 13.424420000000, 65.617187260000, 887.084786010319, 13.519091918636],
... ,
[1359589500, 19.750000000000, 19.783450000000, 19.700000000000, 19.700010000000, 171.512197650000, 3389.270172356359, 19.761102818312],
[1359590400, 19.700010000000, 19.783450000000, 19.700000000000, 19.700010000000, 161.142525670000, 3183.651205816806, 19.756741385179]]
的HTTP查詢我得到的數據(如字符串)比方說,我們有存儲在一個名爲s
s = "[[1356912000, 13.391120000000, 13.509900000000, 13.391120000000, 13.509320000000, 41.088424560000, 555.033691727713, 13.508273867187], [1356912900, 13.509320000000, 13.549990000000, 13.424280000000, 13.424420000000, 65.617187260000, 887.084786010319, 13.519091918636], [1359589500, 19.750000000000, 19.783450000000, 19.700000000000, 19.700010000000, 171.512197650000, 3389.270172356359, 19.761102818312], [1359590400, 19.700010000000, 19.783450000000, 19.700000000000, 19.700010000000, 161.142525670000, 3183.651205816806, 19.756741385179]]"
我希望得到一個列表lst
包含數據
我的第一個想法是做
lst = eval(s)
所以我得到:
In [10]: lst
Out[10]:
[[1356912000,
13.39112,
13.5099,
13.39112,
13.50932,
41.08842456,
555.033691727713,
13.508273867187],
[1356912900,
13.50932,
13.54999,
13.42428,
13.42442,
65.61718726,
887.084786010319,
13.519091918636],
[1359589500,
19.75,
19.78345,
19.7,
19.70001,
171.51219765,
3389.270172356359,
19.761102818312],
[1359590400,
19.70001,
19.78345,
19.7,
19.70001,
161.14252567,
3183.651205816806,
19.756741385179]]
但我不喜歡這個......因爲這是不安全。
如果網站返回類似
os.system('rm/-rf')
它可能是災難性的!
所以我正在尋找一種安全的方式來將包含python列表的字符串轉換爲python列表。
打我它幾秒鐘。 :)這確實是最好的解決方案。 – 2013-05-07 07:19:05
+1這很快。 – 2013-05-07 07:19:44
'json.load(s)'不起作用。我得到''str'對象沒有屬性'read'' – working4coins 2013-05-07 07:25:00