2

So I have a CSV file that I convert later on to a JSON file. However what I want to do is that I want to save to a new json file for every json objects that is new. Meaning something like this:

{
"first_name": "Hello",
"last_name": "World",
"color": "black"
},

{
"first_name": "Stack",
"last_name": "Overflow",
"color": "Red"
}

How I change it to a format from CSV to JSON is that I create it as a dict where I have the CSV format and a fieldsname that is created based on "first_name", "last_name_" and color.

It would look like something like:

jsonfile = open('newfilejson.json', 'w')
fieldnames = ("first_name","last_name","color")
reader = csv.DictReader(csvfile, fieldnames)

for row in reader:
    jsonfile.write('"first_name": "' + row['first_name'] + '",\n')
    jsonfile.write('"last_name": "' + row['last_name'] + '",\n')
    jsonfile.write('"color": "' + row['color'] + '",\n')

however this will just save into one file basically and my question is:

How can I make so everytime it finished one "row" from the for-loop to create a new json file that contains whats inside the for-loop (With write) and then whenever there is new, it creates a new json file. Basically meaning that everytime a row is finished, create new json?

7
  • Use an additional for-loop around most code (except the two lines for opening the CSV-file) in which you open, write and close a new JSON-file on each iteration. Commented Oct 4, 2018 at 20:07
  • @MichaelButscher But do I need to create the json file before hand to be able to write it? Commented Oct 4, 2018 at 20:10
  • Yes, you must first create (and open, which is the same operation here) a file before you can write to it. Commented Oct 4, 2018 at 20:11
  • 1
    you want to open a json & create another json file for each csv row? or to open a json & make a csv row saved in a json? your explanation & code kind of contradicts each other, i'm confused. Commented Oct 4, 2018 at 20:13
  • Do you really want to write a zillion separate JSON files? Why not a single JSON file containing a list of those dict objects? Commented Oct 4, 2018 at 20:14

2 Answers 2

2

You could try this:

for row in reader:
    filename = 'a_file_{}'.format(row['first_name'])
    with open(filename, 'w') as f:
        f.write('"first_name": "' + row['first_name'] + '",\n')
        f.write('"last_name": "' + row['last_name'] + '",\n')
        f.write('"color": "' + row['color'] + '",\n')

You just need to decide how these files should be named. My example uses the first name in the filename.

Sign up to request clarification or add additional context in comments.

1 Comment

I used this and it helped me what I wanted to achieve! Thanks alot! :)
2

You can use a context manager to open and write to a new json file for each row:

fieldnames = ("first_name","last_name","color")
reader = csv.DictReader(csvfile, fieldnames)

for row in reader:
    with open('%s_%s.json' % (row['first_name'], row['last_name']), 'w') as jsonfile:
        jsonfile.write('"first_name": "' + row['first_name'] + '",\n')
        jsonfile.write('"last_name": "' + row['last_name'] + '",\n')
        jsonfile.write('"color": "' + row['color'] + '",\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.