0
import csv
import json
csv_file = 'data.csv'
json_path = 'data.json'

data1 = {}

with open(csv_file) as data_csv:
    csvRead = csv.DictReader(data_csv)
    for csvrows in csvRead:
        id = csvrows['row_id']
        data[id] = csvrows

# Creating Multiple JSON files of 1000 rows each
i = 0
while i < len(data):
    data_file = dict(list(data.items())[j:j+1000])
    with open(json_path + "data" + str(j) + ".json", 'w') as data_json:
        data_json.write(json.dumps(data1, indent=4))
    i = i + 1000

The problem is I get output as

[ "1": { "id": 1, "name": "Smitraj", "Lastname":"Raut" } ] 

But I don't want it in this format. I need the format as

[{ "id": 1, "name": "Smitraj", "Lastname":"Raut" } ]
5
  • 1
    The JSON you have posted is invalid and not the result of the code given. Commented Dec 31, 2019 at 7:14
  • could you show your csv file pl..? Commented Dec 31, 2019 at 7:40
  • The second json is not valid. It's easy to modify your code to output like: { "1": {"name": "Smitraj","Lastname": "Raut"}}. Would that be ok? Commented Dec 31, 2019 at 9:02
  • I want this format[ { "id": 1, "name": "Smitraj", "Lastname":"Raut" } ] but what I get is extra index in the start [ "1": { "id": 1, "name": "Smitraj", "Lastname":"Raut" Commented Dec 31, 2019 at 9:08
  • The output you say you get cannot result from this code and is not valid JSON. The code contains a variable data1 which is not related to anything else in here, but that could not produce the output you claim you get, either. Commented Dec 31, 2019 at 9:20

2 Answers 2

1

Try this. Instead of a dictionary append you items to a list and write that to file.

import csv
import json
csv_file = 'data.csv'
json_path = 'data.json'

data = []

with open(csv_file) as data_csv:
    csv_reader = csv.DictReader(data_csv)
    for csvrows in csv_reader:
        data.append(csvrows)

with open(json_path, 'w') as data_json:
    data_json.write(json.dumps(data,indent=4))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I have done the same code for the per 1000 rows... Thanks for you contribution.
0
import csv
import json

csv_path = 'D:/test/data.csv'
json_path = 'D:/test/'

x=[]

with open(csv_path) as data_csv:
    data_csvRead = csv.DictReader(data_csv)
    for data_csvrows in data_csvRead:
        x.append(data_csvrows)

len(x)


j=0
while j<len(x):
    data = x[j:j+1000]
    with open(json_path+"data_file"+str(j)+".json", 'w') as datarument_json:
        datarument_json.write(json.dumps(data,indent=4))
    j=j+1000

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.