将json文件转换为csv文件

时间:2018-09-20 20:09:10

标签: json csv

我正在尝试将JSON文件中的数据转换为CSV文件。但是我遇到了错误

TypeError: expected string or buffer

import csv, json, sys, urllib
if sys.argv[1] is not None and sys.argv[2] is not None:
 fileInput = sys.argv[1]
 fileOutput = sys.argv[2]

 inputFile = open(fileInput, "w")
 outputFile = open(fileOutput)
 data = json.loads(inputFile)
 inputFile.close()

 output = csv.writer(outputFile)
 output.writerow(data[0].keys())
 for row in data:
    output.writerow(row.values())

我的JSON文件包含以下代码:

 {\"voterId\":\"voterid5\",\"electionId\":\"electionid5\",
\"docType\":\"objecttype5\",\"countyName\":\"countyname5\",    \"questionId\":4,
\"questionText\":\"questiontext4\",
\"ballotId\":4,\"responseId\":4,\"electionStartdate\":9052018,
\"electionEnddate\":9062018,\"voterAge\":21}

0 个答案:

没有答案