使用Python将json转换为csv
我想我要做的很简单。我正在将.json转换为.csv文件,但很难得到我想要的。我肯定我很傻,但在这个网站上找不到答案。请指出我的愚蠢错误是什么 我正在使用以下Python代码:使用Python将json转换为csv,python,json,csv,Python,Json,Csv,我想我要做的很简单。我正在将.json转换为.csv文件,但很难得到我想要的。我肯定我很傻,但在这个网站上找不到答案。请指出我的愚蠢错误是什么 我正在使用以下Python代码: import csv, json inputFile = open('results_20190214b.txt') outputFile = open('results_20190214.csv', 'w', newline='') data = json.load(inputFile) inputFile.clo
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如下所示:
{
"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,但我没有得到任何值。例如:
AccumulatedNewCapacity[UTOPIA,E01,1999]
AccumulatedNewCapacity[UTOPIA,E01,2000]
AccumulatedNewCapacity[UTOPIA,E01,2001]
非常感谢您的任何想法。谢谢 试试这个:
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)
您需要迭代这些行,正如本文所述 因此,您可以执行以下操作
for row in data.items():
output.writerow(row)
但是,请记住,您将结束元组,结果类似于CSV
"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}
我想您只想在borders中加入一部分,以获得累积的新容量和CSV的价值
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上获得以下结果,因为它是作为字符串而不是列表写入的
"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
因此,您需要将值转换为列表并将值附加到列表中
import csv, json
inputFile = open('results.txt')
outputFile = open('results_20190214.csv', 'w', newline='\n')
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()
编辑:忘记添加结果。结果如下所示
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建议代码的一个轻微变体:
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)