从文件加载嵌套列表的最佳方法是什么?我的文件看起来像这样:
[["ar","archers",3],
["aw","arrows",3],
["bd","baby dragon",4],
["ba","barbarians",5],
["bh","barbarian hut",7],
["bt","bomb tower",5],
["bm","bomber",3],
["bw","bowler",5],
["ba","balloon",5],
["br","battle ram",4],
["cl","clone",3],
["ca","cannon",3],
["dg","dart goblin",3],
["dp","dark prince",4],
["ew","electro wizard",4],
["ex","executioner",5],
["eb","elite barbarians",6],
["ec","elixir collector",6]]
答案 0 :(得分:5)
可以json.load()
加载,原样如下:
import json
with open("input.txt") as f:
data = json.load(f)
print(data)
演示:
>>> import json
>>>
>>> with open("input.txt") as f:
... data = json.load(f)
... print(data)
...
[['ar', 'archers', 3], ['aw', 'arrows', 3], ['bd', 'baby dragon', 4], ['ba', 'barbarians', 5], ['bh', 'barbarian hut', 7], ['bt', 'bomb tower', 5], ['bm', 'bomber', 3], ['bw', 'bowler', 5], ['ba', 'balloon', 5], ['br', 'battle ram', 4], ['cl', 'clone', 3], ['ca', 'cannon', 3], ['dg', 'dart goblin', 3], ['dp', 'dark prince', 4], ['ew', 'electro wizard', 4], ['ex', 'executioner', 5], ['eb', 'elite barbarians', 6], ['ec', 'elixir collector', 6]]