Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the error I obtain while running the below code.

Traceback (most recent call last):
  File "E:\Anaconda Projects\Mongo Projects\SDR Tool\csvtojson.py", line 16, in <module>
    jsonString = json.dumps(jsonArray, indent=1, separators=(",", ":"))
  File "C:\Users\Predator\anaconda3\lib\json\__init__.py", line 234, in dumps
    return cls(
  File "C:\Users\Predator\anaconda3\lib\json\encoder.py", line 201, in encode
    chunks = list(chunks)
MemoryError


What I have tried:

import csv
import json
from pymongo import MongoClient

url = "mongodb://localhost:27017"
client = MongoClient(url)
db = client.Office
customer = db.Customer
jsonArray = []

with open("Names.txt", "r") as csv_file:
    csv_reader = csv.DictReader(csv_file, dialect='excel', delimiter='|', quoting=csv.QUOTE_NONE)
    for row in csv_reader:
        jsonArray.append(row)
    jsonString = json.dumps(jsonArray, indent=1, separators=(",", ":"))
    jsonfile = json.loads(jsonString)
    customer.insert_many(jsonfile)
Posted
Updated 14-Jan-22 22:44pm
v2
Comments
Richard MacCutchan 15-Jan-22 4:31am    
Are you sure that the last line
customer.insert_many(jsonString)

should be using jsonString rather than jsonfile?
Cyber Noob 15-Jan-22 4:34am    
:-) That was really some carelessness. Let me run the code again. Thanks for pointing out. Update- The issue persists.
Richard MacCutchan 15-Jan-22 5:38am    
The issue may well have something to do with the data you are inputting. You should test the code with some fixed data that you know is correct. If that works then you know that it is something in the csv file.
Maciej Los 16-Jan-22 11:08am    
Visweswaran N 16-Jan-22 22:42pm    
Mind posting sample content present in the txt file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900