user2355903
user2355903

Reputation: 643

Parsing a jsonl file into a useful structure

I am importing a jsonl file from my hard drive and trying to get it into a usable format. Here is how I'm importing the data.

train_data=[]
with open("Documents/data/train.jsonl",'r',encoding='utf-8') as j:
   for line in j:
           train_data.append(json.loads(line))

Which produces data structured like this.

train_data[1]
Out[59]: 
{'id': 46971,
 'img': 'img/46971.png',
 'label': 1,
 'text': 'text'}

Basically I would like to convert this data to a dictionary format where the dictionary value is the "id" and the rest of the data is associated with that dictionary label. I believe something like the following, but I'm pretty new to Python so I may be displaying this incorrectly.

print(dict_ex)
{46971: ['img/46971.png', 1, 'text']}

Upvotes: 1

Views: 2398

Answers (3)

ilyankou
ilyankou

Reputation: 1329

You can create a dictionary and add new elements from train_data list one by one:

di = dict()
for o in train_data:
    di[o['id']] = [o['img'], o['label'], o['text']]

print(di)
>>> {46971: ['img/46971.png', 1, 'text']}

Upvotes: 1

sushanth
sushanth

Reputation: 8302

Try this,

result = {}
for d in train_data:
    for k, v in d.items():
        if k == "id":
            result[v] = []
        else:
            result[v].append(v)

Upvotes: 0

Nick Chu
Nick Chu

Reputation: 36

# dict[key] = value
dict_ex[data['id']] = [data['img'], data['label'], data['text']]

Upvotes: 0

Related Questions