从CSV创建JSON数据

webghufk  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(111)

我有一个CSV文件,其中包含以下数据:enter image description here我正在使用pandas和python。
我希望JSON数据如下:

{    
       "VMName": "vm101", 
       "tags": [
        {"scope": "Environment", "tag": "Production"}, 
        {"scope": "Application", "tag": "App1"}, 
        {"scope": "Tier", "tag": "Web"}
        ]
       "VMName": "vm102", 
       "tags": [
        {"scope": "Environment", "tag": "Production"}, 
        {"scope": "Application", "tag": "App1"}, 
        {"scope": "Tier", "tag": "App"}
        ]
       "VMName": "vm103", 
       "tags": [
        {"scope": "Environment", "tag": "Production"}, 
        {"scope": "Application", "tag": "App1"}, 
        {"scope": "Tier", "tag": "DB"}
        ]
       "VMName": "vm104", 
       "tags": [
        {"scope": "Environment", "tag": "UAT"}, 
        {"scope": "Application", "tag": "App1"}, 
        {"scope": "Tier", "tag": "Web"}
        ]
       "VMName": "vm105", 
       "tags": [
        {"scope": "Environment", "tag": "UAT"}, 
        {"scope": "Application", "tag": "App1"}
        ]
       "VMName": "vm106", 
       "tags": [
        {"scope": "Environment", "tag": "UAT"}, 
        {"scope": "Application", "tag": "App1"}, 
        {"scope": "Tier", "tag": "DB"}
        ]
}

字符串
如何使用Pandas实现这一点

xiozqbni

xiozqbni1#

首先,您的期望的JSON无效,如果您需要CSV中的有效JSON,您可以这样做,而无需使用pandas

import csv
import json

csv_file = open('data.csv', 'r')
json_file = open('data.json', 'w')

fieldnames = ("Name", "Environment", "Application", "Tier")
reader = csv.DictReader(csv_file, fieldnames)

vm_list = []
for row in reader:
    if row["Name"] != "Name":  # skip the header row
        vm = {}
        vm["VMName"] = row["Name"]
        vm["tags"] = [
            {"scope": "Environment", "tag": row["Environment"]},
            {"scope": "Application", "tag": row["Application"]}
        ]
        if row["Tier"]:
            vm["tags"].append({"scope": "Tier", "tag": row["Tier"]})
        vm_list.append(vm)

json.dump(vm_list, json_file, indent=4)

csv_file.close()
json_file.close()

字符串

data.csv

Name,Environment,Application,Tier
vm101,Production,App1,Web
vm102,Production,App1,App
vm103,Production,App1,DB
vm104,UAT,App1,Web
vm105,UAT,App1,
vm106,UAT,App1,DB

data.json

[
  {
    "VMName": "vm101",
    "tags": [
      {
        "scope": "Environment",
        "tag": "Production"
      },
      {
        "scope": "Application",
        "tag": "App1"
      },
      {
        "scope": "Tier",
        "tag": "Web"
      }
    ]
  },
  {
    "VMName": "vm102",
    "tags": [
      {
        "scope": "Environment",
        "tag": "Production"
      },
      {
        "scope": "Application",
        "tag": "App1"
      },
      {
        "scope": "Tier",
        "tag": "App"
      }
    ]
  },
  {
    "VMName": "vm103",
    "tags": [
      {
        "scope": "Environment",
        "tag": "Production"
      },
      {
        "scope": "Application",
        "tag": "App1"
      },
      {
        "scope": "Tier",
        "tag": "DB"
      }
    ]
  },
  {
    "VMName": "vm104",
    "tags": [
      {
        "scope": "Environment",
        "tag": "UAT"
      },
      {
        "scope": "Application",
        "tag": "App1"
      },
      {
        "scope": "Tier",
        "tag": "Web"
      }
    ]
  },
  {
    "VMName": "vm105",
    "tags": [
      {
        "scope": "Environment",
        "tag": "UAT"
      },
      {
        "scope": "Application",
        "tag": "App1"
      }
    ]
  },
  {
    "VMName": "vm106",
    "tags": [
      {
        "scope": "Environment",
        "tag": "UAT"
      },
      {
        "scope": "Application",
        "tag": "App1"
      },
      {
        "scope": "Tier",
        "tag": "DB"
      }
    ]
  }
]

相关问题