Skip to content

Instantly share code, notes, and snippets.

@killertilapia
Created September 25, 2019 05:56
Show Gist options
  • Save killertilapia/660072ed806c4a920df9cad3c3a07731 to your computer and use it in GitHub Desktop.
Save killertilapia/660072ed806c4a920df9cad3c3a07731 to your computer and use it in GitHub Desktop.
Python example script working with with files and MongoDB or CosmosDB
import csv
from tqdm import tqdm
from pymongo import MongoClient
def remapper():
url = 'SRV_URL'
client = MongoClient(url) # get connection
print('Opening Client')
db = client['bz-parts-data'] # get database
print('Quering Documents')
docs = db.parts_data.find({'field': 'value'}) # get documents
print('Processing Documents')
with open('output_file.csv', mode='w') as csv_file:
field_names = ['fieldname1', 'fieldname1', 'fieldname1']
# if you need a writer
writer = csv.DictWriter(csv_file, fieldnames=field_names) # writer
writer.writeheader() # write fieldnames as headers
for doc in tqdm(docs):
# do whatever to documents
if __name__ == "__main__":
remapper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment