0

I wanted to read tar.json file, so I write:

import json
with open('tar.json', "r", encoding='utf-8') as read_file:
          data = json.load(read_file) 

and print it as dictionary, for which the key will be "linia" (written as int) and the value will be value tuple.

For example, for "linia 52" I want to get data in format:

{52: ('Czerwone Maki P+R', 'Chmieleniec', 'Kampus UJ', 'Ruczaj', 'Norymberska', 'Grota-Roweckiego', 'Lipińskiego', 'Kobierzyńska', 'Słomiana', 'Kapelanka', 'Szwedzka', 'Rondo Grunwaldzkie', 'Orzeszkowej', 'Stradom', 'Starowiślna', 'Poczta Główna', 'Teatr Słowackiego', 'Lubicz', 'Rondo Mogilskie', 'Cystersów', 'Białucha', 'TAURON Arena Kraków Wieczysta', 'Muzeum Lotnictwa', 'AWF', 'Stella-Sawickiego', 'Czyżyny', 'Rondo Czyżyńskie', 'Os. Kolorowe', 'Plac Centralny im. R.Reagana', 'Os. Zgody', 'Rondo Kocmyrzowskie im. Ks. Gorzelanego', 'DH Wanda', 'Rondo Hipokratesa', 'Dunikowskiego', 'Kleeberga', 'Piasta Kołodzieja')}

How to do this? And how to find for every key number of values tuples?

I know that command: >>> data["linia"][19]["przystanek"][1]["name"]

returns

'Chmieleniec 02'
Malum Phobos
  • 115
  • 6
  • What have you tried so far? It looks like the value of key `linia` is a list of dictionaries. You probably want to *iterate* over those entries, formatting them as appropriate. "Iterate" means "you need a loop of some sort". – larsks Oct 22 '22 at 01:14

1 Answers1

0

This should give you what you need.

import json
with open('tar.json', "r", encoding='utf-8') as read_file:
          data = json.load(read_file) 
          out = dict()
          for item in data['linia']:
            keys = list(item.keys())
            x = []
            for i in item[keys[1]]:
                x.append(i['name'])
            out[item[keys[0]]] = x
            print(out)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
spramuditha
  • 357
  • 2
  • 9
  • Additional note there are trailing digits in every name; like 01 02 etc. if you want to remove them instead of appending i['name'] directly make the necessary changes and then append. – spramuditha Oct 22 '22 at 01:50
  • I get an error: `line 8, in for i in item[keys[1]]: IndexError: list index out of range ` – Malum Phobos Oct 22 '22 at 20:26
  • This is probably because one item in your List of dicts not having any corresponding name_list in the dictionary. To counter this you can just put a conditional to check if keys has 2 items. ```if Len(keys) < 2: continue``` – spramuditha Oct 23 '22 at 01:49
  • Now it don't return error but it print with repetitions, i. e. it firstly print "linia 1:", then "linia 1:", "linia 3:" and so on, instead of printing all "linia 1:", "linia 3:", ... at once. – Malum Phobos Oct 23 '22 at 11:55
  • The dictionary should contain "linia" 's number as key in format int. – Malum Phobos Oct 23 '22 at 11:57
  • Unindent print(out). I printed out because you needed that as the output according to question. Instead of printing out['1']. Out's format is dict. You just have to index to extract the output you need. Hope you have some understanding about python dict data structure. – spramuditha Oct 23 '22 at 14:17
  • what you see is it's always printing 1 is because it's always printing the whole out dict starting from first key which is '1' – spramuditha Oct 23 '22 at 14:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248997/discussion-between-spramuditha-and-malum-phobos). – spramuditha Oct 23 '22 at 14:40
  • How to print "linia 1:", "linia 3:", ... at once, without repetition? – Malum Phobos Oct 25 '22 at 23:10
  • out['1'] will print what's depicted in line 1; so use something like ```print("linia",num,":",out[str(num)]``` Again, please check the information related to python dicts. – spramuditha Oct 26 '22 at 04:10