1

I have been trying to convert a csv file into json with python 3. I want to convert a csv file which would look more or less like this:

Key ,Field_1 ,Field_2 ...
0   ,a       ,e       ...
1   ,b       ,f       ...
2   ,c       ,g       ...
3   ,d       ,h       ...

Into a json file that is structured Key : [Field_1, Field_2, ...]

So that the csv above would end up looking like

{
  "0" : ["a", "e", ...]
  "1" : ["b", "f", ...]
  "2" : ["c", "g", ...]
  "3" : ["d", "h", ...]
}

I have been trying to use the code below but have been unable to fill in the missing parts, like assigning each value to their corresponding part on the json file.

csv = pd.read_csv("file.csv")
n = 0
length = # number of keys
for i in csv:
    if n == 0:
        for y in range(lentgh):
            # Assign as the header
    else:
        for y in range(length):
            # Assign as the properties
    n += 1

Also Im trying to make it so that all new data is attached at the end of the json file automatically.

1
  • pandas.Dataframe.to_json transforms a dataframe to JSON and has several parameters that can modify the format. Have you considered one of these formats instead of your own? Commented Oct 21, 2018 at 18:19

2 Answers 2

2

The json will indent on the value lists still but this will convert your csv to the dictionary you desire and append it to json file

import csv
import json

with open('blah.csv') as f:
    reader = csv.reader(f)
    next(reader)
    d = dict((rows[0], rows[1:]) for rows in reader)

with open('blah.json', 'a') as f:
    json.dump(d, f, indent=4)
Sign up to request clarification or add additional context in comments.

4 Comments

Im getting a problem with the last line, it says "dumps() takes 1 positional argument but 2 positional arguments were given", basically it doesnt let me have both d and f
@PSM oops sorry yep should be dump , you got it :)
One more thing, do you know how I could change that code so that when I use an existing file, instead of the new data being appended in a new dictionary after the new one ( { "old_data":"old_data" }{"new_data":"new_data"} ) I get the new data inside the existing brackets ( {"old_data":"old_data", "new_data":"new_data" } )
@psm sorry just left the house but just from reading that it would seem you would have to read load the old json and then update that dictionary and rewrite the entire file
0

This should do what you want.

import csv
import json

csvfile = open('C:\\your_path\\file.csv', 'r')
jsonfile = open('C:\\your_path\\file.json', 'w')

fieldnames = ("ID","Name","Address","State")
reader = csv.DictReader(csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.