Convering json to csv using Python
我认为我想要做的事情非常简单。 我正在将.json转换为.csv文件,但很难得到我想要的东西。 我确定我很傻,但在这个网站上找不到答案。 请指出我的愚蠢错误!
我正在使用以下Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 | import csv, json inputFile = open('results_20190214b.txt') outputFile = open('results_20190214.csv', 'w', newline='') data = json.load(inputFile) inputFile.close output = csv.writer(outputFile) for row in data: output.writerow([row]) outputFile.close() |
我的json看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "AccumulatedNewCapacity[UTOPIA,E01,1999]": { "Value": 0.0225798659394097 }, "AccumulatedNewCapacity[UTOPIA,E01,2000]": { "Value": 0.149302162579271 }, "AccumulatedNewCapacity[UTOPIA,E01,2001]": { "Value": 0.354595177554284 }, "AccumulatedNewCapacity[UTOPIA,E01,2002]": { "Value": 0.553976916527268 }, "AccumulatedNewCapacity[UTOPIA,E01,2003]": { "Value": 0.749394931502283 }, ETC |
此代码成功将字段名称打印到CSV,但我没有获得任何值。 例如:
1 2 3 | AccumulatedNewCapacity[UTOPIA,E01,1999] AccumulatedNewCapacity[UTOPIA,E01,2000] AccumulatedNewCapacity[UTOPIA,E01,2001] |
任何想法都非常感激。 谢谢!
您需要迭代行,因为它也在这里描述;https://stackoverflow.com/a/26660785/11110555
因此,您可以执行以下操作
1 2 | for row in data.items(): output.writerow(row) |
但是,请记住,您将结束元组,就像跟随CSV一样;
1 2 3 4 5 | "AccumulatedNewCapacity[UTOPIA,E01,1999]",{'Value': 0.0225798659394097} "AccumulatedNewCapacity[UTOPIA,E01,2000]",{'Value': 0.149302162579271} "AccumulatedNewCapacity[UTOPIA,E01,2001]",{'Value': 0.354595177554284} "AccumulatedNewCapacity[UTOPIA,E01,2002]",{'Value': 0.553976916527268} "AccumulatedNewCapacity[UTOPIA,E01,2003]",{'Value': 0.749394931502283} |
我认为你想要只占用AccumulatedNewCapacity和Value to CSV的边框
1 2 3 4 5 6 7 8 9 10 11 12 | for row in data.items(): # Getting the first part of AccumulatedNewCapacity basePart = row[0] #Extracting text inside borders baseStr = basePart[basePart.find("[")+1:basePart.find("]")] # Getting the Value part valuePart = row[1] #Extracting value valueStr = valuePart['Value'] output.writerow([baseStr,valueStr]) |
这将导致您在CSV上有以下结果,因为它被写为字符串而不是列表。
1 2 3 4 5 | "UTOPIA,E01,1999",0.0225798659394097 "UTOPIA,E01,2000",0.149302162579271 "UTOPIA,E01,2001",0.354595177554284 "UTOPIA,E01,2002",0.553976916527268 "UTOPIA,E01,2003",0.749394931502283 |
因此,您需要将值转换为列表并将值附加到其中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import csv, json inputFile = open('results.txt') outputFile = open('results_20190214.csv', 'w', newline=' ') data = json.load(inputFile) inputFile.close output = csv.writer(outputFile) for row in data.items(): # Getting the first part of AccumulatedNewCapacity basePart = row[0] # Extracting text inside borders baseStr = basePart[basePart.find("[")+1:basePart.find("]")] # Splitting values with comma and turning into list writeStr = baseStr.split(",") # Getting the Value part valuePart = row[1] #Extracting value valueStr = valuePart['Value'] # Adding value to the list writeStr.append(valueStr) # Writing into CSV output.writerow(writeStr) outputFile.close() |
编辑:忘记添加结果。 这将是如下结果
1 2 3 4 5 | UTOPIA,E01,1999,0.0225798659394097 UTOPIA,E01,2000,0.149302162579271 UTOPIA,E01,2001,0.354595177554284 UTOPIA,E01,2002,0.553976916527268 UTOPIA,E01,2003,0.749394931502283 |
感谢您的回复 - 非常有帮助。
我实际上也希望将变量名保留在csv中,所以最后得到了Koray B的建议代码的轻微变体:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | for row in data.items(): variablePart = row[0] variableStr = variablePart[:variablePart.find("[")] writeStr = [] writeStr.append(variableStr) variableIndexStr = variablePart[variablePart.find("[")+1:variablePart.find("]")] variableIndexStrSplit = variableIndexStr.split(",") for i in variableIndexStrSplit: indexStr = i writeStr.append(indexStr) valuePart = row[1] valueStr = valuePart['Value'] writeStr.append(valueStr) output.writerow(writeStr) |
试试这个:
1 2 3 4 5 6 7 8 | import csv with open('results_20190214b.txt', 'r') as in_file: stripped = (line.strip() for line in in_file) lines = (line.split(",") for line in stripped if line) with open('results_20190214.csv', 'w') as out_file: writer = csv.writer(out_file) writer.writerow(( 'title', 'intro')) # here enter your rows titles writer.writerows(lines) |