解析并将JSON转换为CSV

时间:2017-05-16 22:20:37

标签: json python-3.x csv data-conversion

Parsing values from a JSON file using Python?的帮助下,我能够使用以下方式读取数据:

with open('data/data.json') as data_file:
    data_loaded = json.load(data_file)

但是,我无法进入下一步,我希望将其转换为CSV文件。示例代码:

import pandas as pd
import json 
from io import StringIO
import io
import csv
import sys

sample = {'Table': [{'AA12_Requestor_0': 'John Doe',
   'AA28_Requestor Business Unit_0': 'Manchester United',
   'AA29_Target Remediation Date_0': '2017-07-01',
   'AA29_Exception Lead_0': 'Ji Sung Park',
   'AA29_Level 1 Approver_0': 'Phil Knight',
   'AA29_Date of last WF change_0': '2017-02-12',
   'AA29_Approval Cycles_0': '1',
   'AA29_Level 2 Approver_0': 'Jae In Moon',
   'AA29_Exception Submitted Date_0': '2015-10-12',
   'AA29_Days in Awaiting Assignment_0': '0',
   'AA29_Days in Awaiting Info Sec Review_0': '0',
   'AA29_Days in Awaiting Governance Management Review_0': '2',
   'AA29_Days in Awaiting Level 1 Review - No Level 2 Rev_0': '',
   'AA29_Days from Submitted to Approved_0': '5',
   'AA29_Days from Submitted to Rejected_0': '',
   'AA29_Geography_0': None,
   'AA29_Blood Type': 'Love',
   'AA32_EX: Area_0': None,
   'AA32_Impact_0': None,
   'AA32_Rating_0': None,
   'AA32_Rating (Number)_0': '',
   'AA32_Likelihood_0': None,
   'AA32_Risk Sensitivity_0': None,
   'AA3_Description_0': 'Title: Get your job done | Norm: Be happy.\xa0 Love OS. | Blood type: Z',
   'AA39_Record ID_0': '12345',
   'AA39_Escalation Flag_0': 'No',
   'AA39_Submitted Date_0': '2015-10-15',
   'AA40_ID_0': '123',
   'AF13_Record Workflow State': 'Canceled',
   'FINDING_ID': 12345}]}

我的JSON文件中还有更多上述字典的迭代

我试过了:

  1. 参考http://blog.appliedinformaticsinc.com/how-to-parse-and-convert-json-to-csv-using-python/

    json_parsed = json.loads(sample)
    

    给我一​​个错误,说JSON对象必须是字符串而不是字典。

    TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
    
  2. 尝试创建csvwriter对象

    csvwriter = csv.writer(sample)
    

    给我一​​个错误说:

    TypeError: argument 1 must have a "write" method
    
  3. 我还阅读How can I convert JSON to CSV?但我无法通过第一步,即json.loads(sample)

  4. 根据Python - JSON to CSV table? DictWriter听起来很有意思。但我不知道如何将其应用到我的任务中。

  5. pd.read_json(sample)也期待字符串。提出错误

    TypeError: Expected String or Unicode
    
  6. 我希望所有键都是标题(列)和行中键的所有值。

1 个答案:

答案 0 :(得分:1)

sample不是JSON对象。

pd.read_json将会失败。

pd.read_json接受一个指向JSON文档的字符串。你正在传递一个dict对象。

尝试传递此信息:

sample = {'AA12_Requestor_0': 'John Doe',
   'AA28_Requestor Business Unit_0': 'Manchester United',
   'AA29_Target Remediation Date_0': '2017-07-01',
   'AA29_Exception Lead_0': 'Ji Sung Park',
   'AA29_Level 1 Approver_0': 'Phil Knight',
   'AA29_Date of last WF change_0': '2017-02-12',
   'AA29_Approval Cycles_0': '1',
   'AA29_Level 2 Approver_0': 'Jae In Moon',
   'AA29_Exception Submitted Date_0': '2015-10-12',
   'AA29_Days in Awaiting Assignment_0': '0',
   'AA29_Days in Awaiting Info Sec Review_0': '0',
   'AA29_Days in Awaiting Governance Management Review_0': '2',
   'AA29_Days in Awaiting Level 1 Review - No Level 2 Rev_0': '',
   'AA29_Days from Submitted to Approved_0': '5',
   'AA29_Days from Submitted to Rejected_0': '',
   'AA29_Geography_0': None,
   'AA29_Blood Type': 'Love',
   'AA32_EX: Area_0': None,
   'AA32_Impact_0': None,
   'AA32_Rating_0': None,
   'AA32_Rating (Number)_0': '',
   'AA32_Likelihood_0': None,
   'AA32_Risk Sensitivity_0': None,
   'AA3_Description_0': 'Title: Get your job done | Norm: Be happy.\xa0 Love OS. | Blood type: Z',
   'AA39_Record ID_0': '12345',
   'AA39_Escalation Flag_0': 'No',
   'AA39_Submitted Date_0': '2015-10-15',
   'AA40_ID_0': '123',
   'AF13_Record Workflow State': 'Canceled',
   'FINDING_ID': 12345}

data = pd.DataFrame(sample, index=[0]) # Only one row so index has a length of 1

从那里你可以执行```data.to_csv(“path / to / out / file.csv”)``将JSON对象传递给csv。