Created
June 5, 2013 18:57
-
-
Save gadamc/5716256 to your computer and use it in GitHub Desktop.
Example on how to read a CSV file using DictReader and then upload them to couchdb (with couchdbkit)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from couchdbkit import Server | |
from csv import DictReader | |
import time, sys, subprocess, math, os, datetime, pytz, calendar, json | |
#______________ | |
# parseDoc | |
def parseDoc(doc): | |
for k,v in doc.items(): | |
#strips off any spaces found in the keys. | |
del doc[k] | |
kk = k.strip(' ') | |
doc[kk] = v | |
# see if this string is really an int or a float | |
if (isinstance(v,str) or isinstance(v, unicode)): | |
if v.isdigit()==True: #int | |
doc[kk] = int(v) | |
else: #try a float | |
try: | |
if math.isnan(float(v))==False: | |
doc[kk] = float(v) | |
except: | |
pass | |
return doc | |
#______________ | |
# uploadFile | |
def uploadFile(fname, uri, dbname): | |
print 'Upload contents of %s to %s/%s' % (fname, uri, dbname) | |
# #connect to the db | |
theServer = Server(uri) | |
db = theServer[dbname] | |
#loop on file for upload | |
reader = DictReader(open(fname, 'rU'), dialect = 'excel') | |
#store a list of docs for upload to db | |
docs = [] | |
for doc in reader: | |
newdoc = parseDoc(doc) | |
#enforce some sort of schema. that is, require the existence of a set of keys in the database documents | |
#requiredSet = set(['muonmodule', 'end', 'year', 'month', 'day', 'HV channel', 'ADC channel', 'ADC card', 'TDC channel']) | |
#if (requiredSet < set(newdoc.keys())) is False: | |
# print 'Quitting! Your file MUST have the following columns' | |
# print ', '.join([x for x in requiredSet]) | |
# sys.exit(1) | |
#Do any extra formatting of 'newdoc' or add new fields here. | |
#append to list for bulk upload when ready | |
docs.append(newdoc) | |
db.bulk_save(docs) | |
if __name__=='__main__': | |
csvfile = sys.argv[1] | |
uri = sys.argv[2] | |
dbname = sys.argv[3] | |
uploadFile(csvfile, uri, dbname) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment