sharp
sharp

Reputation: 2158

python: csv to json create elements separated by comma

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 "]} 

Upvotes: 2

Views: 2085

Answers (3)

Sam
Sam

Reputation: 228

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))

Upvotes: 0

Enayat
Enayat

Reputation: 4042

 for row in reader:
     row['team_members'] = row['team_members'].split(',')
     json.dump(row, jsonfile)
     jsonfile.write('\n')

Upvotes: 0

Martijn
Martijn

Reputation: 417

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.

Upvotes: 2

Related Questions