我认为我想做的事情很简单。我正在将.json转换为.csv文件,但很难获得我想要的东西。我确定我很傻,但是在此网站上找不到答案。请指出我的愚蠢错误是什么!
我正在使用以下Python代码:
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]
任何想法都非常感谢。谢谢!
答案 0 :(得分:1)
您需要迭代行,如此处所述。 https://stackoverflow.com/a/26660785/11110555
因此,您可以执行以下操作
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}
我想您想为CSV的AccumulatedNewCapacity和Value添加边框
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
因此,您需要将值转换为list并将值附加到其中。
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
答案 1 :(得分:0)
尝试一下:
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)
答案 2 :(得分:0)
感谢您的回复-非常有用。
我实际上也想将变量名也保留在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)