Guest User

Untitled

a guest
Nov 20th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import csv
  2. import json
  3. import pymongo
  4. import sys
  5.  
  6. host = "dbmongo01.decisyon.office"
  7. port = 27017
  8. database = "data_scrm"
  9. username = "scrm"
  10. password = "scrm"
  11.  
  12. def pop_collection(cname, csvfile):
  13.     conn = pymongo.Connection(host, port)
  14.     db = conn[database]
  15.  
  16.     if db.authenticate(username, password):
  17.         collection = db[cname]
  18.        
  19.         row = csvfile.next()
  20.        
  21.         for item in csvfile:
  22.             docname = {}
  23.  
  24.             docname = {row[0] : item[0],
  25.                        row[1] : json.loads(item[1]),
  26.                        row[2] : item[2]
  27.                        }
  28.             collection.insert(docname)
  29.     conn.close()
  30.  
  31. filename = open(sys.argv[1], 'r')
  32. collname = filename.name.split('\\')[-1:][0].split('.')[0]
  33.  
  34. pop_collection(collname, csv.reader(filename, delimiter='\t'))
Add Comment
Please, Sign In to add comment