-4

我从网站上抓取了一些数据,然后将其保存在 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 "],
  ....
  ....
]

非常感谢 !!

4

2 回答 2

4
import json

with open('json_file') as f:
    data = json.load(f)
于 2013-06-16T01:55:42.120 回答
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 ']]
于 2013-06-16T02:01:32.147 回答