python: csv to json create elements separated by comma

134
April 30, 2018, at 10:11 PM

I am able to convert the csv to json file.Right now, I can create JSON file through the field names, see current output section.

There are multiple name in "team_members" field that has been separated by ','. How do I convert the team_members names into a Element through python?

Data: (demo.csv)

id,team_name,team_members
123,Biology,"Ali Smith, Jon Doe"
234,Math,Jane Smith 
345,Statistics ,"Matt P, Albert Shaw"
456,Chemistry,"Andrew M, Matt Shaw, Ali Smith"
678,Physics,"Joe Doe, Jane Smith, Ali Smith "

Python Code:

import csv
import json
csvfile = open('files/csv/input/demo.csv', 'r')
jsonfile = open('files/csv/input/demo.csv', 'w')
fieldnames = ("id",
              "team_name",
              "team_members")
reader = csv.DictReader( csvfile) # without headers
# reader = csv.DictReader( csvfile, fieldnames) # with headers
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')

Current Output: Gives the json file with fields

{"id": "123", "team_name": "Biology", "team_members": "Ali Smith, Jon Doe"}
{"id": "234", "team_name": "Math", "team_members": "Jane Smith "}
{"id": "345", "team_name": "Statistics ", "team_members": "Matt P, Albert Shaw"}
{"id": "456", "team_name": "Chemistry", "team_members": "Andrew M, Matt Shaw, Ali Smith"}
{"id": "678", "team_name": "Physics", "team_members": "Joe Doe, Jane Smith, Ali Smith "}
Final Output Needed:
{"id": "123", "team_name": "Biology", "team_members": ["Ali Smith", "Jon Doe"]}
{"id": "234", "team_name": "Math", "team_members": ["Jane Smith"]},
{"id": "345", "team_name": "Statistics ", "team_members": ["Matt P", "Albert Shaw"]}
{"id": "456", "team_name": "Chemistry", "team_members": ["Andrew M", "Matt Shaw", "Ali Smith"]}
{"id": "678", "team_name": "Physics", "team_members": ["Joe Doe", "Jane Smith", "Ali Smith "]} 
Answer 1
with open(csvfilepath) as infile, open('outfile.json','w') as outfile: 
    for line in infile: 
         row = dict()
         id, team_name, *team_members = line.split(',')
         row["id"] = id; row["team_name"] = team_name; row["team_members"] = team_members
         outfile.write(json.dumps(row))
Answer 2

You might want to check out string.split to solve this issue for you:

row['team_members'] = row['team_members'].split(', ')

This will replace the "team_members" field by an array split over the commas.

Answer 3
 for row in reader:
     row['team_members'] = row['team_members'].split(',')
     json.dump(row, jsonfile)
     jsonfile.write('\n')
READ ALSO
Is it possible to update a Google Forms with Google Sheets data [on hold]

Is it possible to update a Google Forms with Google Sheets data [on hold]

I have a Google Form that Students enter their Student Id, Fname, Lname & their broken Chromebook infoI have another Google Form(CB form) that has what was done to that student's Chromebook

30
Adding on Click marker on Google Maps to calculate distance to marker

Adding on Click marker on Google Maps to calculate distance to marker

I'm trying to add an on-click event when I click on a place on the map it get the user's location then they click on a marker and it calculates a route and displays distanceThis is my code so far, which aims to add the marker to map when the user clicks

141
CSS tool for generating gradients [on hold]

CSS tool for generating gradients [on hold]

When creating background gradients I use this tool to generate CSS for the color and angle I like: https://cssgradientio/

159