我正在尝试将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}