1

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']}
user2355903
  • 593
  • 2
  • 8
  • 29

3 Answers3

1

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']}
ilyankou
  • 1,309
  • 8
  • 13
0
# dict[key] = value
dict_ex[data['id']] = [data['img'], data['label'], data['text']]
Nick Chu
  • 36
  • 4
0

Try this,

result = {}
for d in train_data:
    for k, v in d.items():
        if k == "id":
            result[v] = []
        else:
            result[v].append(v)
sushanth
  • 8,275
  • 3
  • 17
  • 28