-
-
Save mprajwala/849b5909f5b881c8ce6a to your computer and use it in GitHub Desktop.
#!/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) |
Thanks for sharing 🎉 😃
Thank you, this was very helpful!
I need to store values of each column in a different variable (parsing). So that modifications on particular column values are possible before loading to the DB. How can this be done?
thank you for this example, was killing myself to do this seemingly basic thing
@akamath03 did you ever get the solution to your question? 'twould help me out a lot right now.
I am getting an error NameError: name 'file' is not defined - what should I be defining here?
awesome, thanks
Thanks for your help !
i need to map a csv which has an id field already to be imported to mongo with the _id as this id field.How to do that?
(rhis csv file has an id field)
try:
client = MongoClient(mongouri)
database = client['automation']
database.create_collection('testTableMongo'+str(suffix))
coll = database['testTableMongo'+str(suffix)]
data = pd.read_csv(os.path.abspath('../data/' + file))
payload = json.loads(data.to_json(orient='records'))
coll.remove()
coll.insert(payload)
except OperationFailure as msg:
print("Command skipped: %s", msg)
I'm with the error "name 'file' is not defined"
I'm with the error "name 'file' is not defined"
Hey, kindly post a snippet of your code for easier help
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
I need help to load data into mongo database with collection creation from worksheet which contain multiple excel sheets
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')
Thank you! This helped tons!