【问题标题】:How to control node names when converting CSV to JSON in python在python中将CSV转换为JSON时如何控制节点名称
【发布时间】:2019-06-27 16:03:53
【问题描述】:

我在 python 中使用了一个简单的 csv 到 json 脚本,并且我已经成功地将 CSV 转换为 JSON...

csvFilePath = ("x.csv")

# Read the CSV and add the data to a dictionary
data = {}
with open(csvFilePath) as csvFile:
    csvReader = csv.DictReader(csvFile)
    for csvRow in csvReader:
        objectid = csvRow["objectid"]
        data[objectid] = csvRow
        d = csvrow

# Converts dictionary to JSON list
json_string = json.dumps([data], indent = 4)

生成的 JSON 如下所示...

[
{
    "15494": {
        "status": "Open", 
        "site_name": "Healthpromed Municipality of Vieques", 
        "objectid": "15494", 
        "utilities": "No", 
        "site_dashboard_surrogate_key": "BPS-H80-019569", 
        "supplies": "No", 
        "structural": "No", 
        "staff": "No"
    }, 
    "1": {
        "status": "Closed", 
        "site_name": "PUBLIC HEALTH AND HUMAN SERVICES, MONTANA DEPARTMENT OF", 
        "objectid": "1", 
        "utilities": "No", 
        "site_dashboard_surrogate_key": "010478A2-14EF-4B7B-84A0-2618F311D82F", 
        "supplies": "No", 
        "structural": "No", 
        "staff": "No"
    }
}]

我需要帮助更改由“objectid”设置为“attributes”的节点名称(即“15494”、“1”)

我想要的代码应该是这样的......

[
{
    "attributes": {
        "status": "Open", 
        "site_name": "Healthpromed Municipality of Vieques", 
        "objectid": "15494", 
        "utilities": "No", 
        "site_dashboard_surrogate_key": "BPS-H80-019569", 
        "supplies": "No", 
        "structural": "No", 
        "staff": "No"
    }, 
    "attributes": {
        "status": "Closed", 
        "site_name": "PUBLIC HEALTH AND HUMAN SERVICES, MONTANA DEPARTMENT OF", 
        "objectid": "1", 
        "utilities": "No", 
        "site_dashboard_surrogate_key": "010478A2-14EF-4B7B-84A0-2618F311D82F", 
        "supplies": "No", 
        "structural": "No", 
        "staff": "No"
    }
}]

有什么想法吗?我在这里束手无策。我尝试在 for 循环中将 objectid = csvRow["objectid"] 替换为 ["attributes"] 但它只是覆盖了第一个条目。

【问题讨论】:

  • 其实你期望的json无效,会通过Duplicate key "attributes"
  • 这就是我正在使用的 REST API 在请求参数下接受(developers.arcgis.com/rest/services-reference/…)的内容
  • 你检查你想要的 json 是否是有效的 json 格式?

标签: python json csv


【解决方案1】:

未经过测试,但我想这是您根据 ArcGIS REST API 文档所需要的。希望对你有帮助:)

csvFilePath = ("x.csv")

# Read the CSV and add the data to a dictionary
data = [] # make it a list
object = {} # initialize a dictionary
with open(csvFilePath) as csvFile:
    csvReader = csv.DictReader(csvFile)
    for csvRow in csvReader:
        object['attributes'] = csvRow["objectid"] # create dictionary
        data.append(dict(object)); # append every dictionary to list
        d = csvrow

# Converts dictionary to JSON list
json_string = json.dumps(data, indent = 4) # make json dump your list of dictionary

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2016-11-05
    • 2019-05-28
    • 2012-05-27
    • 2020-05-31
    • 1970-01-01
    • 2021-09-09
    相关资源
    最近更新 更多