The JSON-file looks like this
[["bla", "bla",] ["bla2", "bla2"] [..] [..]]
The JSON-file consists of hundreds of these list.
I need to get the ["bla", "bla"]
part out of the list and make it an named tuple.
how can I do this?
The JSON-file looks like this
[["bla", "bla",] ["bla2", "bla2"] [..] [..]]
The JSON-file consists of hundreds of these list.
I need to get the ["bla", "bla"]
part out of the list and make it an named tuple.
how can I do this?
You can use the json
library for this:
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)