将字符串转换为元组python列表

时间:2013-06-16 01:54:40

标签: python json

我从网站上删除了一些数据,然后将其保存在json文件中。 当我在另一个代码中调用json文件时,它变成了一个像这样的长字符串

'[[" Up to 300 g ", " 900 yen ", " 1,200 yen ", " 1,500 yen ", " 1,700 yen "], [" Up to 500 g ", " 1,100 yen ", " 1,500 yen ", " 1,800 yen ", " 2,100 yen "], [" Up to 600 g ", " 1,240 yen ", " 1,680 yen ", " 2,000 yen ", " 2,440 yen "], [" Up to 700 g ", " 1,380 yen ", " 1,860 yen ", " 2,200 yen ", " 2,780 yen "] ................ 

如何将整个事物转换为元组列表

[ 
  [" Up to 300 g ", " 900 yen ", " 1,200 yen ", " 1,500 yen ", " 1,700 yen "],
  [" Up to 500 g ", " 1,100 yen ", " 1,500 yen ", " 1,800 yen ", " 2,100 yen "],
  [" Up to 600 g ", " 1,240 yen ", " 1,680 yen ", " 2,000 yen ", " 2,440 yen "],
  ....
  ....
]

非常感谢!!

2 个答案:

答案 0 :(得分:4)

import json

with open('json_file') as f:
    data = json.load(f)

答案 1 :(得分:1)

>>> import ast
>>> ast.literal_eval('[[" Up to 300 g ", " 900 yen ", " 1,200 yen ", " 1,500 yen ", " 1,700 yen "], [" Up to 500 g ", " 1,100 yen ", " 1,500 yen ", " 1,800 yen ", " 2,100 yen "]]')
[[' Up to 300 g ', ' 900 yen ', ' 1,200 yen ', ' 1,500 yen ', ' 1,700 yen '], [' Up to 500 g ', ' 1,100 yen ', ' 1,500 yen ', ' 1,800 yen ', ' 2,100 yen ']]