简体   繁体   English

将csv转换为json(嵌套对象)

[英]convert csv to json (nested objects)

I am new to python, and I am having to convert a csv file to json in following format: 我是python的新手,我必须将csv文件转换为以下格式的json:

CSV File : CSV文件:

firstname, lastname, email, customerid, dateadded, customerstatus
john, doe, john.doe@do.com, 124,26/11/18,active
jane, doe, jane.doe@do.com, 125,26/11/18,active

JSON format: JSON格式:

{
    firstname: "John",
    lastname: "Doe",
    emailOrPhone: "john.doe@do.com",
    extraFields: [{
            name: "customerid",
            value: "124"
        },
        {
            name: "dateadded",
            value: "26/11/18"
        },
        {
            name: "dateadded",
            value: "26/11/18"
        }
    ]
}, {
    firstname: "Jane",
    lastname: "Doe",
    emailOrPhone: "Jane.doe@do.com",
    extraFields: [{
            name: "customerid",
            value: "125"
        },
        {
            name: "dateadded",
            value: "26/11/18"
        },
        {
            name: "dateadded",
            value: "26/11/18"
        }
    ]
}


current code I am using:
import requests
import json
import time
import csv
import json
import glob
import os
import logging


for filename in glob.glob('D:\\api\\Extract.csv'):
    csvfile = os.path.splitext(filename)[0]
    jsonfile = csvfile + '.json'

    with open(csvfile+'.csv') as f:
        reader = csv.DictReader(f)
        rows = list(reader)

    with open(jsonfile, 'w') as f:
        json.dump(rows, f)

url = 'api_url'

with open("D:\\api\\Extract.json", "r") as read_file:
    data = json.load(read_file)

    for item in data:


        headers = {"Authorization" : "key", "Content-Type" : "application/json"}

        r = requests.post(url, data= json.dumps(item), headers= headers)



        logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(message)s',
                    handlers=[logging.FileHandler("D:\\api\\log_file.log"),
                              logging.StreamHandler()])

I can produce parent values in json, but I am not sure how do I get sub-nodes and parse column name as values and iterate through entire file like that. 我可以在json中生成父值,但是我不确定如何获取子节点并将列名解析为值并像这样遍历整个文件。 Above code converts csv to simple json objects, I want to achieve nested objects. 上面的代码将csv转换为简单的json对象,我想实现嵌套对象。 I am thinking maybe appending would be the solution, but not sure how to pass column as value and corresponding data as value. 我在想也许追加将是解决方案,但不确定如何将列作为值和相应的数据作为值传递。

You can use csv.DictReader which gives you access to the column name as you're iterating each row. 您可以使用csv.DictReader ,在迭代每一行时可以访问列名。 Then you can build each item as follows: 然后,您可以按以下步骤构建每个项目:

import json
import csv

primary_fields = ['firstname', 'lastname', 'email']
result = []
with open('mydata.csv') as csv_file:
    reader = csv.DictReader(csv_file, skipinitialspace=True)
    for row in reader:
        d = {k: v for k, v in row.items() if k in primary_fields}
        d['extraFields'] = [{'name': k, 'value': v} for k, v in row.items() if k not in primary_fields]
        result.append(d)

print(json.dumps(result, indent=2))

Output 产量

[
  {
    "firstname": "john",
    "lastname": "doe",
    "email": "john.doe@do.com",
    "extraFields": [
      {
        "name": "customerid",
        "value": "124"
      },
      {
        "name": "dateadded",
        "value": "26/11/18"
      },
      {
        "name": "customerstatus",
        "value": "active"
      }
    ]
  },
  {
    "firstname": "jane",
    "lastname": "doe",
    "email": "jane.doe@do.com",
    "extraFields": [
      {
        "name": "customerid",
        "value": "125"
      },
      {
        "name": "dateadded",
        "value": "26/11/18"
      },
      {
        "name": "customerstatus",
        "value": "active"
      }
    ]
  }
]

If you want to set custom field names in your final json (eg emailOrPhone for email ), you can always manually set field names for d and set the appropriate value 如果您想在最终的json中设置自定义字段名称(例如, emailOrPhoneemail ),则始终可以手动为d设置字段名称并设置适当的值

At little more complicated than needs to be, but you can try building your JSON array as you read in your values from the csv file, then output your result to a .json file with json.dump at the end: 在稍微复杂得多,需要的是,但你可以尝试建立您的JSON数组,你在你的价值观从CSV文件中读取,然后输出你的结果到.json与文件json.dump底:

from csv import reader
from json import dump

top_fields = ["firstname", "lastname", "email"]
extra_fields = ["customerid", "dateadded", "customerstatus"]

data = []
with open("customers.csv") as csv_in:
    csv_reader = reader(csv_in)

    # Get headers
    headers = list(map(str.strip, next(csv_reader)))

    for row in csv_reader:
        json_object = {}

        # Build dictionary for each row
        row_map = dict(zip(headers, map(str.strip, row)))

        # Add in top fields first
        for top in top_fields:
            json_object[top] = row_map[top]

        # Then add in extra fields
        for extra in extra_fields:
            json_object.setdefault("extraFields", []).append(
                {"name": extra, "value": row_map[extra]}
            )

        data.append(json_object)

with open("customers.json", "w") as fp:
    dump(data, fp, indent=4, sort_keys=True)

Which gives the following customers.json : 这给出了以下customer.json

[
    {
        "email": "john.doe@do.com",
        "extraFields": [
            {
                "name": "customerid",
                "value": "124"
            },
            {
                "name": "dateadded",
                "value": "26/11/18"
            },
            {
                "name": "customerstatus",
                "value": "active"
            }
        ],
        "firstname": "john",
        "lastname": "doe"
    },
    {
        "email": "jane.doe@do.com",
        "extraFields": [
            {
                "name": "customerid",
                "value": "125"
            },
            {
                "name": "dateadded",
                "value": "26/11/18"
            },
            {
                "name": "customerstatus",
                "value": "active"
            }
        ],
        "firstname": "jane",
        "lastname": "doe"
    }
]
import csv
import sys
import json

#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["firstname","secondname","age"]

def convert(filename):
  csv_filename = filename[0]
  print "Opening CSV file: ",csv_filename 
  f=open(csv_filename, 'r')
  csv_reader = csv.DictReader(f,fieldnames)
  json_filename = csv_filename.split(".")[0]+".json"
  print "Saving JSON to file: ",json_filename
  jsonf = open(json_filename,'w') 
  data = json.dumps([r for r in csv_reader])
  jsonf.write(data) 
  f.close()
  jsonf.close()

if __name__=="__main__":
  convert(sys.argv[1:])

USAGE: 用法:

python csv2json.py myCSVfile.txt python csv2json.py myCSVfile.txt

where myCSVfile.txt it's your CSV file (name it as you prefer). 其中myCSVfile.txt是您的CSV文件(根据您的喜好命名)。

It will create a JSON array in a file named myCSVfile.json 它将在名为myCSVfile.json的文件中创建一个JSON数组。

That's all. 就这样。

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

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