在Python中获取错误以标准化嵌套列表

时间:2018-09-24 14:59:15

标签: python-3.x pandas dictionary dataframe

我有一个带有字典的嵌套列表。以下只是列表的第一个元素

    {'id': 'abcde',
     'authorization': None,
     'operation_type': 'xx',
     'method': 'card',
     'transaction_type': 'asd',
     'card': {'type': 'dd',
      'brand': 'vv',
      'address': {'line1': 'xxxxxxx',
       'line2': '',
       'line3': '',
       'state': 'xx',
       'city': 'xxx',
       'postal_code': '12345',
       'country_code': 'xx'},
      'card_number': '123456XXXXXX7890',
      'holder_name': 'name user,
      'expiration_year': '20',
      'expiration_month': '02',
      'allows_charges': True,
      'allows_payouts': True,
      'bank_name': 'abc bank',
      'bank_code': '000'},
     'status': 'fgh',
     'conciliated': True,
     'creation_date': '2018-09-23T23:58:17-05:00',
     'operation_date': '2018-09-23T23:58:17-05:00',
     'description': 'asdmdefdsa',
     'error_message': 'sdaskjflj',
     'order_id': 'ashdgjasdfhk',
     'amount': 418.0,
     'customer': {'name': 'abc',
      'last_name': 'xyz',
      'email': 'abcdef@hotmail.com',
      'phone_number': '12345678',
      'address': None,
      'creation_date': '2018-09-23T23:58:18-05:00',
      'external_id': None,
      'clabe': None},
     'fee': {'amount': 0.56, 'tax': 0.91, 'currency': 'XXX'},
     'currency': 'XXX'},
{'id': 'abcde',
     'authorization': None,
     'operation_type': 'xx',
     'method': 'card',
     'transaction_type': 'asd',
     'card': {'type': 'dd',
      'brand': 'vv',
      'address': {'line1': 'xxxxxxx',
       'line2': '',
       'line3': '',
       'state': 'xx',
       'city': 'xxx',
       'postal_code': '12345',
       'country_code': 'xx'},
      'card_number': '123456XXXXXX7890',
      'holder_name': 'name user,
      'expiration_year': '20',
      'expiration_month': '02',
      'allows_charges': True,
      'allows_payouts': True,
      'bank_name': 'abc bank',
      'bank_code': '000'},
     'status': 'fgh',
     'conciliated': True,
     'creation_date': '2018-09-23T23:58:17-05:00',
     'operation_date': '2018-09-23T23:58:17-05:00',
     'description': 'asdmdefdsa',
     'error_message': 'sdaskjflj',
     'order_id': 'ashdgjasdfhk',
     'amount': 418.0,
     'customer': {'name': 'abc',
      'last_name': 'xyz',
      'email': 'abcdef@hotmail.com',
      'phone_number': '12345678',
      'address': None,
      'creation_date': '2018-09-23T23:58:18-05:00',
      'external_id': None,
      'clabe': None},
     'fee': {'amount': 0.56, 'tax': 0.91, 'currency': 'XXX'},
     'currency': 'XXX'}

我想将数据标准化为数据框。我将代码编写为:json_normalize(d)。但是我遇到以下错误:

  

-------------------------------------------------- ---------------------------- KeyError Traceback(最近的呼叫   最后)在()   ----> 1 df = json_normalize(data)

     

/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py在   json_normalize(数据,record_path,meta,meta_prefix,record_prefix,   错误,9月)       201#TODO:处理记录值是列表,至少有错误       202#合理   -> 203 data = nested_to_record(data,sep = sep)       204返回DataFrame(数据)       205 elif not isinstance(record_path,list):

     

/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py在   nested_to_record(ds,前缀,sep,级别)        其他86个        87 v = new_d.pop(k)   ---> 88 new_d.update(嵌套_记录(v,newkey,sep,level + 1))        89 new_ds.append(new_d)        90

     

/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py在   nested_to_record(ds,前缀,sep,级别)        82 new_d [newkey] = v        83如果v为None:#弹出键,如果值为None   ---> 84 new_d.pop(k)        85继续        其他86个:

     

KeyError:“地址”

我了解到,因为地址为None,所以代码给了我错误。但是我不知道如何解决。在这方面的任何帮助将不胜感激。提前致谢。 (请注意,该数据是伪数据)

2 个答案:

答案 0 :(得分:5)

字典格式错误。首先,您将看到以下内容:

'holder_name': 'name user,

其中值'name user不是有效的字符串,因为它的右边没有用单引号引起来。

第二,在您的代码中,列表中有两个元素,即两个字典,每个字典都以{'id': ...开头,而不是声明的单个元素。

在两个字典中都将'holder_name的值固定并使其成为一个由两个成员组成的列表之后,您可以继续使用json_normalize,并且将获得类似以下的输出(在stdout中打印) :

   amount authorization card.address.city card.address.country_code    ...        operation_type      order_id status transaction_type 0  
418.0          None               xxx                        xx       ...                    xx  ashdgjasdfhk    fgh              asd 1  
418.0          None               xxx                        xx       ...                    xx  ashdgjasdfhk    fgh              asd

[2 rows x 42 columns]

答案 1 :(得分:3)

我试图重现此错误,但无法。创建python3 venv并使用pip安装熊猫后,我将您的代码(python字典而不是json-我的错,谢谢我@AlessandroCosentino +1)复制到编辑器,发现第16和56行缺少单引号 'holder_name': 'name user,  并且应该是 'holder_name': 'name user',

from pandas.io.json import json_normalize
data = {'id': 'abcde',
        'authorization': None,
        'operation_type': 'xx',
        'method': 'card',
        'transaction_type': 'asd',
        'card': {'type': 'dd',
        'brand': 'vv',
        'address': {'line1': 'xxxxxxx',
        'line2': '',
        'line3': '',
        'state': 'xx',
        'city': 'xxx',
        'postal_code': '12345',
        'country_code': 'xx'},
        'card_number': '123456XXXXXX7890',
        'holder_name': 'name user',
        'expiration_year': '20',
        'expiration_month': '02',
        'allows_charges': True,
        'allows_payouts': True,
        'bank_name': 'abc bank',
        'bank_code': '000'},
        'status': 'fgh',
        'conciliated': True,
        'creation_date': '2018-09-23T23:58:17-05:00',
        'operation_date': '2018-09-23T23:58:17-05:00',
        'description': 'asdmdefdsa',
 'error_message': 'sdaskjflj',
 'order_id': 'ashdgjasdfhk',
 'amount': 418.0,
 'customer': {'name': 'abc',
  'last_name': 'xyz',
  'email': 'abcdef@hotmail.com',
  'phone_number': '12345678',
  'address': None,
  'creation_date': '2018-09-23T23:58:18-05:00',
  'external_id': None,
  'clabe': None},
 'fee': {'amount': 0.56, 'tax': 0.91, 'currency': 'XXX'},
 'currency': 'XXX'},
  {'id': 'abcde',
 'authorization': None,
 'operation_type': 'xx',
 'method': 'card',
 'transaction_type': 'asd',
 'card': {'type': 'dd',
  'brand': 'vv',
  'address': {'line1': 'xxxxxxx',
   'line2': '',
   'line3': '',
   'state': 'xx',
   'city': 'xxx',
   'postal_code': '12345',
   'country_code': 'xx'},
  'card_number': '123456XXXXXX7890',
  'holder_name': 'name user',
  'expiration_year': '20',
  'expiration_month': '02',
  'allows_charges': True,
  'allows_payouts': True,
  'bank_name': 'abc bank',
  'bank_code': '000'},
 'status': 'fgh',
 'conciliated': True,
 'creation_date': '2018-09-23T23:58:17-05:00',
 'operation_date': '2018-09-23T23:58:17-05:00',
 'description': 'asdmdefdsa',
 'error_message': 'sdaskjflj',
 'order_id': 'ashdgjasdfhk',
 'amount': 418.0,
 'customer': {'name': 'abc',
  'last_name': 'xyz',
  'email': 'abcdef@hotmail.com',
  'phone_number': '12345678',
  'address': None,
  'creation_date': '2018-09-23T23:58:18-05:00',
  'external_id': None,
  'clabe': None},
 'fee': {'amount': 0.56, 'tax': 0.91, 'currency': 'XXX'},
 'currency': 'XXX'}


print(json_normalize(data))

输出是这个 a screenshot from my term

这可以通过使用带有python高亮显示的智能编辑器(例如SublimeText)轻松避免。您正在使用什么编辑器?