how to push a csv data to mongodb using python

Thank you for the suggestion.This one is the corrected code:

import csv
import json
import pandas as pd
import sys, getopt, pprint
from pymongo import MongoClient
#CSV to JSON Conversion
csvfile = open('C://test//final-current.csv', 'r')
reader = csv.DictReader( csvfile )
mongo_client=MongoClient() 
db=mongo_client.october_mug_talk
db.segment.drop()
header= [ "S No", "Instrument Name", "Buy Price", "Buy Quantity", "Sell Price", "Sell Quantity", "Last Traded Price", "Total Traded Quantity", "Average Traded Price", "Open Price", "High Price", "Low Price", "Close Price", "V" ,"Time"]

for each in reader:
    row={}
    for field in header:
        row[field]=each[field]

    db.segment.insert(row)

Why do you insert data one by one? Take a look at this one.

import pandas as pd
from pymongo import MongoClient

client = MongoClient(<your_credentials>)
database = client['YOUR_DB_NAME']
collection = database['your_collection']

def csv_to_json(filename, header=None):
    data = pd.read_csv(filename, header=header)
    return data.to_dict('records')

collection.insert_many(csv_to_json('your_file_path'))

Please be aware of that it might crash your app when the file is too big.


The easiest way is by using pandas my code is

import json
import pymongo
import pandas as pd
myclient = pymongo.MongoClient()

df = pd.read_csv('yourcsv.csv',encoding = 'ISO-8859-1')   # loading csv file
df.to_json('yourjson.json')                               # saving to json file
jdf = open('yourjson.json').read()                        # loading the json file 
data = json.loads(jdf)                                    # reading json file 

now you can insert this json in your mangodb database :-]