Home > Mobile >  Python append entry KeyError problem because of missing data from the API
Python append entry KeyError problem because of missing data from the API

Time:12-31

So, i'm trying to collect data from an API to make a dataframe. The problems is that that when i get the response in JSON some of the values are missing for some rows. That means that one row has all 10 out of 10 values and some only have 8 out of 10. For e.g. I have such code to fill in the data from the API to then form a DataFrame:

response = r.json()
cols = ['A', 'B', 'C', 'D']
l = []
for entry in response:
    l.append([
        entry['realizationreport_id'],
        entry['suppliercontract_code'],
        entry['rid'],
        entry['ppvz_inn'],

I get this error because in one of the rows the API didn't give a value in response:

KeyError: 'ppvz_inn'

So i'm tryng to fix it so that the cell of the DataFrame is filled with 0 or Nan if the API doesn't have a value for this specific row

l = []
for entry in response:
    l.append([
        entry['realizationreport_id'],
        entry['suppliercontract_code'],
        entry['rid'],
        entry['ppvz_inn'],
        try:
          entry['ppvz_supplier_name'],
        except KeyError:
            '0',

And now i get this error:

try:
  ^
SyntaxError: invalid syntax

How to actually make it work and fill those cells with no data?

CodePudding user response:

You cannot have a try-except statement in the middle of your append statement.

You could either work with if statements or first try to fix your JSON data by filling in empty values. You could also maybe use setdefault, see here some info about it.

CodePudding user response:

Use collections.defaultdict. It's a subclass of dict which does not return KeyError, creating a called key instead. You can cast your existing dict to defaultdict using unpacking.

for entry in response:
  entry_defaultdict = defaultdict(list, **entry)

In this case, every call to non-existing object will create an empty list as a value of the key.

  • Related