简体   繁体   中英

Convert CSV file to JSON with python

I am trying to covert my CSV email list to a JSON format to mass email via API. This is my code thus far but am having trouble with the output. Nothing is outputting on my VS code editor.

import csv
import json


def make_json(csvFilePath, jsonFilePath):

    data = {}

    with open(csvFilePath, encoding='utf-8') as csvf:
        csvReader = csv.DictReader(csvf)

        for rows in csvReader:

            key = rows['No']
            data[key] = rows

    with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
        jsonf.write(json.dumps(data, indent=4))


csvFilePath = r'/data/csv-leads.csv'
jsonFilePath = r'Names.json'

make_json(csvFilePath, jsonFilePath)

Here is my desired JSON format

  {
        "EmailAddress": "hello@youngstowncoffeeseattle.com",
        "Name": "Youngstown Coffee",
        "ConsentToTrack": "Yes"
    },

Heres my CSV list

No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen & Bakery,catering@zylberschtein.com,Yes
Youngstown Coffee,hello@youngstowncoffeeseattle.com,Yes

It looks like you could use a csv.DictReader to make this easier.

If I have data.csv that looks like this:

Name,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen,catering@zylberschtein.com,yes
Youngstown Coffee,hello@youngstowncoffeeseattle.com,yes

I can convert it into JSON like this:

>>> import csv
>>> import json
>>> fd = open('data.csv')
>>> reader = csv.DictReader(fd)
>>> print(json.dumps(list(reader), indent=2))
[
  {
    "Name": "Zylberschtein's Delicatessen",
    "EmailAddress": "catering@zylberschtein.com",
    "ConsentToTrack": "yes"
  },
  {
    "Name": "Youngstown Coffee",
    "EmailAddress": "hello@youngstowncoffeeseattle.com",
    "ConsentToTrack": "yes"
  }
]

Here I've assumed the headers in the CSV can be used verbatim. I'll update this with an exmaple if you need to modify key names (eg convert "No" to "Name"),.


If you need to rename a column, it might look more like this:

import csv
import json


with open('data.csv') as fd:
    reader = csv.DictReader(fd)
    data = []
    for row in reader:
        row['Name'] = row.pop('No')
        data.append(row)

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

Given this input:

No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen,catering@zylberschtein.com,yes
Youngstown Coffee,hello@youngstowncoffeeseattle.com,yes

This will output:

[
  {
    "EmailAddress": "catering@zylberschtein.com",
    "ConsentToTrack": "yes",
    "Name": "Zylberschtein's Delicatessen"
  },
  {
    "EmailAddress": "hello@youngstowncoffeeseattle.com",
    "ConsentToTrack": "yes",
    "Name": "Youngstown Coffee"
  }
]

and to print on my editor is it simply print(json.dumps(list(reader), indent=2)) ?

I'm not really familiar with your editor; print is how you generate console output in Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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