简体   繁体   English

Python 将 csv 转换为具有特定格式的 json

[英]Python convert csv to json with specific format

I am trying to write a python script that takes the following structured CSV file:我正在尝试编写一个采用以下结构化 CSV 文件的 python 脚本:

"id","city","diacritice","county","auto","zip","populatie","lat","lng"
"1","Buftea","Buftea","Ilfov","IF","70000","19202","44.5629744","25.9388214"
"2","Buciumeni","Buciumeni","Ilfov","IF","70000","2976","44.5460939","25.9574846"
"3","Otopeni","Otopeni","Ilfov","IF","75100","12540","44.5671874","26.0835113"
"4","Odaile","Odăile","Ilfov","IF","75100","1321","44.5435944","26.0487028"

and converts it to the following json file:并将其转换为以下 json 文件:

{
  "locations": [
    {
      "name": "New York",
      "slug": "",
      "description": "",
      "meta": [
        {"image_id" :  45},
        {"_icon" :  ""}
      ],
      "order": 0,
      "child": [
        {"name": "New York", "order": 1 },
        {"name" : "Port Chester", "order": 2},
        {"name" : "Mineola", "order": 3},
        {"name" : "Mount Vernon", "order": 4},
        {"name" : "Hempstead", "order": 5},
        {"name" : "Lynbrook", "order": 6},
        {"name" : "Yonkers", "order": 7},
        {"name" : "Franklin Square", "order": 8},
      ]
    }
  ]
}

The main location elements should be the country names and the child elements should be the city names.主要位置元素应该是国家名称,子元素应该是城市名称。

I have wrote the following script, which in first step reads the whole css file, after that it puts all the counties in a set, in the second loop it iterates over all the counties and somehow I wanted to put an other for loop inside the json declaration, but it was a bad idea.我写了下面的脚本,它在第一步读取整个 css 文件,然后将所有县放在一个集合中,在第二个循环中它遍历所有县,不知何故我想在json 声明,但这是个坏主意。

import csv
import json

# First, parse the CSV data using the csv module

county = set()

with open('localitati.csv', 'r', newline='') as csvfile:
  # Use the csv.reader function to read the data line by line
  reader = csv.reader(csvfile)

  # Loop through each line of the CSV data
  for row in reader:
    # Print the name from the second column of the CSV data
    #print(row[3])
    county.add(row[3])

for cou in county:
    csvData = '"1","Buftea","Ilfov","IF","70000","19202","44.5629744","25.9388214"'

    parsedData = list(csv.reader([csvData]))

    # Next, construct the JSON object using the parsed data
    jsonData = {
      "locations": [
        {
          "name": cou,
          "slug": "",
          "description": "",
          "meta": [
            {"image_id" : ""},
            {"_icon" : ""}
          ],
          "order": "",
          "child": [
              #here i tired the for loop to get the cities 
            {"name": parsedData[0][1], "order": ""},
            {"name": parsedData[0][1], "order": ""},
          ]
        }
      ]
    }

# Finally, output the JSON object
print(json.dumps(jsonData, indent=2))

with open("sample.json", "w") as outfile:
    outfile.write(json.dumps(jsonData, indent=2))

You need to gather up all of the counties into a dictionary before you can convert them to a list.在将它们转换为列表之前,您需要将所有县收集到字典中。

import csv
import json

counties = {}

for row in csv.DictReader(open('x.csv')):
    if row['county'] not in counties:
        counties[row['county']] = {
            'name': row['county'],
            'slug': '',
            'description': '',
            'order': 0,
            'child': [
                {'name': row['city'], 'order': 1}
            ]
        }
    else:
        idx = len(counties[row['county']]['child'])+1
        counties[row['county']]['child'].append(
            {'name': row['city'], 'order': idx}
        )

data = {'locations':list(counties.values())}
print(json.dumps(data, indent=4))

Output:输出:

{
    "locations": [
        {
            "name": "Ilfov",
            "slug": "",
            "description": "",
            "order": 0,
            "child": [
                {
                    "name": "Buftea",
                    "order": 1
                },
                {
                    "name": "Buciumeni",
                    "order": 2
                },
                {
                    "name": "Otopeni",
                    "order": 3
                },
                {
                    "name": "Odaile",
                    "order": 4
                }
            ]
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM