python - 使用 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.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]
任何想法都非常感谢。谢谢!
解决方案
您需要迭代行,因为它也在此处描述;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}
我想您只想参与 AccumulatedNewCapacity 和 Value to 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
推荐阅读
- laravel - 如何验证欺骗输入字段名称的 laravel 输入?
- google-cloud-platform - Dataprep vs Dataflow vs Dataproc
- r - R Shiny:使用 ShinyWidget pickerInput 在 Leaflet 中取消选择图层
- php - 在 laravel 中按相关表排序查询
- c# - System.Threading.Tasks.TaskCanceledException 在 C# 中未处理
- kdb - 从面向 Web 的数据库中将 KDB 表下载到 [q] 会话中
- java - 无法将 ArrayList 的 ArrayList 实例化为 Java 中的列表列表
- google-cloud-dataflow - Apache BEAM 管道 IllegalArgumentException - 时间戳倾斜?
- c# - 仅在运行时才实例化其他类的 asp webservice 中的错误
- python - Pygame精灵穿越屏幕