簡體   English   中英

使用python和pandas dataframe將復雜的json轉換為csv

[英]complex json to csv using python and pandas dataframe

我知道這個問題已經被問過很多次了,但我仍然無法將其轉換為json。

我的json文件如下所示:

{
    "itemCostPrices": {
        "Id": 1,
        "costPrices": [{
            "costPrice": 83.56,
            "currencyCode": "GBP",
            "startDateValid": "2010-09-06",
            "endDateValid": "2011-05-01",
            "postCalculatedCostPriceFlag": false,
            "promoCostPriceFlag": true
        }]
    },
    "eventId": null,
    "eventDateTime": null
}

試試這個代碼:

import json
import pandas as pd

def flatten_dict(d, acc={}):
    for k, v in d.items():
        if isinstance(v, dict):
            flatten_dict(v, acc)
        elif isinstance(v, list):
            for l in v:
                flatten_dict(l, acc)
        else:
            acc[k] = v
    return acc


with open('tmp.json') as f:
    data = json.load(f)

df = pd.DataFrame([flatten_dict(d, {}) for d in data])
df.to_csv('tmp.csv', index=False)

代碼說明:

1)閱讀並將您的json文件導入字典:

with open('tmp.json') as f:
        data = json.load(f)

你會得到:

[{'eventDateTime': None,
  'eventId': None,
  'itemCostPrices': {'Id': 1,
                     'costPrices': [{'costPrice': 83.56,
                                     'currencyCode': 'GBP',
                                     'endDateValid': '2011-05-01',
                                     'postCalculatedCostPriceFlag': False,
                                     'promoCostPriceFlag': True,
                                     'startDateValid': '2010-09-06'}]}},
 {'eventDateTime': None,
  'eventId': None,
  'itemCostPrices': {'Id': 2,
                     'costPrices': [{'costPrice': 99.56,
                                     'currencyCode': 'EUR',
                                     'endDateValid': '2017-05-01',
                                     'postCalculatedCostPriceFlag': False,
                                     'promoCostPriceFlag': True,
                                     'startDateValid': '2018-09-06'}]}}]

2)整理字典:

flat_data = [flatten_dict(d, {}) for d in data]

您將獲得以下flatten dict列表:

[{'Id': 1,
  'costPrice': 83.56,
  'currencyCode': 'GBP',
  'startDateValid': '2010-09-06',
  'endDateValid': '2011-05-01',
  'postCalculatedCostPriceFlag': False,
  'promoCostPriceFlag': True,
  'eventId': None,
  'eventDateTime': None},
 {'Id': 2,
  'costPrice': 99.56,
  'currencyCode': 'EUR',
  'startDateValid': '2018-09-06',
  'endDateValid': '2017-05-01',
  'postCalculatedCostPriceFlag': False,
  'promoCostPriceFlag': True,
  'eventId': None,
  'eventDateTime': None}]

3)將字典加載到pandas數據框中

df = pd.DataFrame(flat_data)

你會得到:

   Id  costPrice currencyCode endDateValid eventDateTime eventId  postCalculatedCostPriceFlag  promoCostPriceFlag startDateValid
0   1      83.56          GBP   2011-05-01          None    None                        False                True     2010-09-06
1   2      99.56          EUR   2017-05-01          None    None                        False                True     2018-09-06  

4)將數據框另存為csv

df.to_csv('tmp.csv', index=False)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM