解析复杂的json对象

2024-09-20 22:21:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个yaml文件创建了这个json对象。我试图序列化它,但我得到了错误。在

{'info': {'city': 'Southampton',
  'dates': [datetime.date(2005, 6, 13)],
  'gender': 'male',
  'match_type': 'T20',
  'outcome': {'by': {'runs': 100}, 'winner': 'England'},
  'overs': 20,
  'player_of_match': ['KP Pietersen'],
  'teams': ['England', 'Australia'],
  'toss': {'decision': 'bat', 'winner': 'England'},
  'umpires': ['NJ Llong', 'JW Lloyds'],
  'venue': 'The Rose Bowl'},
 'innings': [{'1st innings': {'deliveries': [{0.1: {'batsman': 'ME Trescothick',
       'bowler': 'B Lee',
       'non_striker': 'GO Jones',
       'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
       {'19.6': {'batsman': 'PD Collingwood',
       'bowler': 'GD McGrath',
       'non_striker': 'J Lewis',
       'runs': {'batsman': 0, 'extras': 0, 'total': 0},
       'wicket': {'fielders': ['RT Ponting'],
        'kind': 'caught',
        'player_out': 'PD Collingwood'}}}],
    'team': 'England'}},
    {'2nd innings': {'deliveries': [{'0.1': {'batsman': 'AC Gilchrist',
       'bowler': 'D Gough',
       'non_striker': 'ML Hayden',
       'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
     {'14.3': {'batsman': 'GD McGrath',
       'bowler': 'SJ Harmison',
       'non_striker': 'MS Kasprowicz',
       'runs': {'batsman': 0, 'extras': 0, 'total': 0},
       'wicket': {'kind': 'bowled', 'player_out': 'GD McGrath'}}}],
    'team': 'Australia'}}]}

任何帮助都将不胜感激。我试图将这个json对象转换成csv文件中的一行。在

我试图运行的代码:

^{pr2}$

错误

ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.


Tags: 对象jsonextras错误runstotalplayernon
1条回答
网友
1楼 · 发布于 2024-09-20 22:21:28

IIUC您可以使用json_normalize()方法:

In [76]: d = {'info': {'city': 'Southampton',
    ...:   'dates': [datetime.date(2005, 6, 13)],
    ...:   'gender': 'male',
    ...:   'match_type': 'T20',
    ...:   'outcome': {'by': {'runs': 100}, 'winner': 'England'},
    ...:   'overs': 20,
    ...:   'player_of_match': ['KP Pietersen'],
    ...:   'teams': ['England', 'Australia'],
    ...:   'toss': {'decision': 'bat', 'winner': 'England'},
    ...:   'umpires': ['NJ Llong', 'JW Lloyds'],
    ...:   'venue': 'The Rose Bowl'},
    ...:  'innings': [{'1st innings': {'deliveries': [{0.1: {'batsman': 'ME Trescothick',
    ...:        'bowler': 'B Lee',
    ...:        'non_striker': 'GO Jones',
    ...:        'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
    ...:        {'19.6': {'batsman': 'PD Collingwood',
    ...:        'bowler': 'GD McGrath',
    ...:        'non_striker': 'J Lewis',
    ...:        'runs': {'batsman': 0, 'extras': 0, 'total': 0},
    ...:        'wicket': {'fielders': ['RT Ponting'],
    ...:         'kind': 'caught',
    ...:         'player_out': 'PD Collingwood'}}}],
    ...:     'team': 'England'}},
    ...:     {'2nd innings': {'deliveries': [{'0.1': {'batsman': 'AC Gilchrist',
    ...:        'bowler': 'D Gough',
    ...:        'non_striker': 'ML Hayden',
    ...:        'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
    ...:      {'14.3': {'batsman': 'GD McGrath',
    ...:        'bowler': 'SJ Harmison',
    ...:        'non_striker': 'MS Kasprowicz',
    ...:        'runs': {'batsman': 0, 'extras': 0, 'total': 0},
    ...:        'wicket': {'kind': 'bowled', 'player_out': 'GD McGrath'}}}],
    ...:     'team': 'Australia'}}]}

In [77]: pd.io.json.json_normalize(d)
Out[77]:
     info.city    info.dates info.gender info.match_type  info.outcome.by.runs info.outcome.winner  info.overs  \
0  Southampton  [2005-06-13]        male             T20                   100             England          20

  info.player_of_match            info.teams info.toss.decision info.toss.winner           info.umpires  \
0       [KP Pietersen]  [England, Australia]                bat          England  [NJ Llong, JW Lloyds]

      info.venue                   innings
0  The Rose Bowl  [{'1st innings': {'de...

相关问题 更多 >

    热门问题