JSON文件看起来像这样
[["bla", "bla",] ["bla2", "bla2"] [..] [..]]
JSON文件包含数百个这样的列表。
我需要从列表中取出["bla", "bla"]
部分并将其命名为元组。
我该怎么做?
答案 0 :(得分:0)
您可以使用json
库:
from collections import namedtuple
import json
with open('file.json', 'r') as f:
lst = json.load(f)
# get the item from the list (and remove it)
item = lst.pop(lst.index(["bla", "bla"]))
# create the namedtuple you need
Strings = namedtuple("strings", ["str1", "str2"])
# generate a named tuple from the item
named_tuple = Strings(*item)