A
A
Andrew2021-11-26 00:18:22
Python
Andrew, 2021-11-26 00:18:22

How to bypass json and handle exceptions?

There is a json file in which, among other things, there may or may not be a career key (also, in this section, not all fields may be filled out):

{"first_name":"Иван","last_name":"Иванов","bdate":"12.12.2020","site":"xxx.ru","occupation":{"id":297,"name":"МФТИ (Физтех)","type":"university"},"career":[{"city":"Москва","company":"Рога и копыта","from":2006,"position":"продавец","until":2010}],"university_name":"МФТИ (Физтех)","faculty_name":"Факультет проблем физики и энергетики","graduation":1995,"education_status":"Выпускник (магистр)"}


I'm trying to bypass the following loop:
for i in response.json()['response']['items']:
            edu = {
                "faculty": i.get('faculty_name'),
                "chair": i.get('university_name'),
                "enter_year": None,
                "graduation_year": i.get('graduation')
            }
            if i.get('career'):
                career = []
                for y in i['career']:
                    career.append(
                        {
                            "company": i['career'][y]['company'],
                            "company_web_site": i.get('site'),
                            # "city": i['career'][y]['city_id'],
                            "employemnt_year": i['career'][y]['from'],
                            "quit_year": i['career'][y]['until'],
                            "position": i['career'][y]['position'],
                            "desc": None
                        }
                    )
            else:
                career = None


I get the following error:
"company": i['career'][y]['company'],
TypeError: list indices must be integers or slices, not dict


what is wrong, where is the error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-11-26
@rsytrade

B yalready contains the data you need, namely the dictionary from career. So that
"company": y['company'],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question