【发布时间】:2021-01-09 15:57:10
【问题描述】:
我需要将包含数百行的 CSV 转换为 JSON,以便我需要重复键值。
这是我用来将其转换为当前状态的内容
#Quest 1
import csv
import json
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['MMSA']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
csvFilePath = r'/home/user/Downloads/mmsa-icu-beds.csv'
jsonFilePath = r'/home/user/Downloads/mmsa-icu-beds.json'
make_json(csvFilePath, jsonFilePath)
我拥有的 CSV 文件是: Current csv
我目前得到的是这种 JSON 格式
{"MMSA": "Manhattan, KS", "total_percent_at_risk": "47.29%", "high_risk_per_ICU_bed": "4489.84875", "high_risk_per_hospital": "8979.6975", "icu_beds": "8", "hospitals": "4", "total_at_risk": "35918.79"}
{"MMSA": "Hilton Head Island-Bluffton-Beaufort, SC", "total_percent_at_risk": "62.72%", "high_risk_per_ICU_bed": "3904.163571", "high_risk_per_hospital": "36438.86", "icu_beds": "28", "hospitals": "3", "total_at_risk": "109316.58"}
{"MMSA": "Kahului-Wailuku-Lahaina, HI", "total_percent_at_risk": "59.13%", "high_risk_per_ICU_bed": "3860.557", "high_risk_per_hospital": "19302.785", "icu_beds": "20", "hospitals": "4", "total_at_risk": "77211.14"}
{"MMSA": "Spartanburg, SC", "total_percent_at_risk": "66.12%", "high_risk_per_ICU_bed": "3786.115556", "high_risk_per_hospital": "85187.6", "icu_beds": "45", "hospitals": "2", "total_at_risk": "170375.2"}
{"MMSA": "Baton Rouge, LA", "total_percent_at_risk": "66.60%", "high_risk_per_ICU_bed": "3459.7325", "high_risk_per_hospital": "39000.62091", "icu_beds": "124", "hospitals": "11", "total_at_risk": "429006.83"}
{"MMSA": "Rockingham County-Strafford County, NH, Metropolitan Division", "total_percent_at_risk": "57.72%", "high_risk_per_ICU_bed": "3365.052", "high_risk_per_hospital": "40380.624", "icu_beds": "60", "hospitals": "5", "total_at_risk": "201903.12"}
{"MMSA": "Salisbury, MD-DE", "total_percent_at_risk": "68.32%", "high_risk_per_ICU_bed": "3292.271176", "high_risk_per_hospital": "37312.40667", "icu_beds": "68", "hospitals": "6", "total_at_risk": "223874.44"}
期待 JSON 格式:
{"MMSA": "Manhattan, KS", "Manhattan, KS total_percent_at_risk": "47.29%", "Manhattan, KS high_risk_per_ICU_bed": "4489.84875", "Manhattan, KS high_risk_per_hospital": "8979.6975", "Manhattan, KS icu_beds": "8", "Manhattan, KS hospitals": "4", "Manhattan, KS total_at_risk": "35918.79"}
【问题讨论】:
-
分享您的转化代码。
-
我已经提过了
-
所以你只期待第一个元素?或者您是否尝试将
MMSA值放在每个键的开头?我希望您尝试获取的格式会使数据更难处理 -
是的,您完全正确,但我想稍后将其提供给 NLP 模型,这是我可以使它更像语言的唯一方法
-
将您的 CSV 文件粘贴为文本而不是图像或链接。
标签: python json python-3.x csv