Here is my code, very simple stuff ...
import csv import json csvfile = open('file.csv', 'r') jsonfile = open('file.json', 'w') fieldnames = ("FirstName","LastName","IDNumber","Message") reader = csv.DictReader( csvfile, fieldnames) out = json.dumps( [ row for row in reader ] ) jsonfile.write(out)
Declare some field names, the reader uses CSV to read the file, and the names that are submitted dump the file in JSON format. Here's the problem ...
Each entry in the CSV file is on a different line. I want the JSON output to be the same. The problem is that he folds it all on one giant long line.
I tried using something like for line in csvfile: and then ran my code below with reader = csv.DictReader( line, fieldnames) , which scrolls along each line, but it makes the whole file on one line and then projects the whole file to another line ... continues until the line ends.
Any suggestions for fixing this?
Edit: To clarify, I currently have: (each entry in line 1)
[{"FirstName":"John","LastName":"Doe","IDNumber":"123","Message":"None"},{"FirstName":"George","LastName":"Washington","IDNumber":"001","Message":"Something"}]
What I'm looking for: (2 entries on 2 lines)
{"FirstName":"John","LastName":"Doe","IDNumber":"123","Message":"None"} {"FirstName":"George","LastName":"Washington","IDNumber":"001","Message":"Something"}
Not every separate field backed / on a separate line, but each record on it has its own line.
Invalid input.
"John","Doe","001","Message1" "George","Washington","002","Message2"