Home > Net >  Why my 2D list object cannot be subscriptable?
Why my 2D list object cannot be subscriptable?

Time:01-27

I've downloaded data from this link: (enter image description here

I tried to find the information on forums, tried to convert coordinate data into a np.array but nothing seems to help.

CodePudding user response:

Some municipalities contain None in their "souradnice" key so you have to check for that:

import requests

url = "https://data.cesko.digital/obce/1/obce.json"

data = requests.get(url).json()

for m in data['municipalities']:
    # some municipalities contain 'null' in "souradnice" key
    # for example "Morkovice-Slížany"
    souradnice = m['souradnice']
    if souradnice is None:
        x, y = None, None
    else:
        x, y = souradnice
    print(m['hezkyNazev'], x, y)

CodePudding user response:

You have missing values in your dataset that causes the bug if they are not dealt with.

x = list()
y = list()

muncipalites=location_data['municipalities']

for z in range(len(muncipalites)):
    if isinstance(muncipalites[z]['souradnice'],list):
        y.append(muncipalites[z]['souradnice'][0])
        x.append(muncipalites[z]['souradnice'][1])
    else:
        print(f"missing data for {muncipalites[z][list(muncipalites[z].keys())[0]]}")
  • Related