-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e22960
commit 8720eb9
Showing
2 changed files
with
15 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
import os | ||
from pymongo import MongoClient | ||
import pandas as pd | ||
# converts mongodb collection to csv | ||
|
||
|
||
def convert_collection_to_df(mongo_cli, collection, match): | ||
def convert_collection_to_df(mongo_cli, collection, field1, match1, | ||
field2, match2): | ||
dbcli = mongo_cli | ||
scrader_db = dbcli['scrader'] | ||
cursor = scrader_db[collection].find(match, {'_id': False}) | ||
cursor = scrader_db[collection].find({'$and': [{field1: {'$in': match1}}, | ||
{field2: {'$in': match2}}]}, | ||
{'_id': False}) | ||
return pd.DataFrame(list(cursor)) | ||
|
||
|
||
if __name__ == '__main__': | ||
dataframe = convert_collection_to_df(MongoClient(), 'dev_articles', | ||
{'checked': True}) | ||
dataframe.to_csv("./TrainingData.csv", encoding='utf-8') | ||
'checked', [True], | ||
'appended', [False]) | ||
# if file does not exist write header | ||
if not os.path.isfile('TrainingData.csv'): | ||
dataframe.to_csv('TrainingData.csv', encoding='utf-8') | ||
else: # else it exists so append without writing the header | ||
dataframe.to_csv('filename.csv', mode='a', header=False, | ||
encoding='utf-8') |