Skip to content

Instantly share code, notes, and snippets.

@mprajwala
Last active July 23, 2023 20:07
Show Gist options
  • Star 72 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save mprajwala/849b5909f5b881c8ce6a to your computer and use it in GitHub Desktop.
Save mprajwala/849b5909f5b881c8ce6a to your computer and use it in GitHub Desktop.
Store CSV data into mongodb using python pandas
#!/usr/bin/env python
import sys
import pandas as pd
import pymongo
import json
def import_content(filepath):
mng_client = pymongo.MongoClient('localhost', 27017)
mng_db = mng_client['mongodb_name'] // Replace mongo db name
collection_name = 'collection_name' // Replace mongo db collection name
db_cm = mng_db[collection_name]
cdir = os.path.dirname(__file__)
file_res = os.path.join(cdir, filepath)
data = pd.read_csv(file_res)
data_json = json.loads(data.to_json(orient='records'))
db_cm.remove()
db_cm.insert(data_json)
if __name__ == "__main__":
filepath = '/path/to/csv/path' // pass csv file path
import_content(filepath)
@Roalpifi
Copy link

Roalpifi commented Jun 9, 2020

I'm with the error "name 'file' is not defined"

@ItsCosmas
Copy link

I'm with the error "name 'file' is not defined"

Hey, kindly post a snippet of your code for easier help

@nathaliadv
Copy link

image

@0xdeepmehta
Copy link

image

when you use file in python interactive mode, this error occur.
So you should write this line os.path.dirname(__file__) in file.py.

for more reference consider this : https://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined

@dasroja
Copy link

dasroja commented Mar 15, 2021

I need help to load data into mongo database with collection creation from worksheet which contain multiple excel sheets

@hakymulla
Copy link

I need help to load data into mongo database with collection creation from worksheet which contain multiple excel sheets

you can read a sheet with pandas with the sheet_name att.
df = pd.read_excel('my_excel.xlsx', sheet_name='Sheet 1')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment