Last active
September 7, 2016 06:40
-
-
Save karimkhanp/c39e6da3159c85fb2352db7389198399 to your computer and use it in GitHub Desktop.
Useful python function
This file contains hidden or 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
#Function to read xlsx files | |
def readExcel(self): | |
from xlrd import open_workbook | |
wb = open_workbook('Shortlisted Rumours Jan to June 2016.xlsx') | |
for s in wb.sheets(): | |
for row in range(1, s.nrows): | |
# pdb.set_trace() | |
col_names = s.row(0) | |
col_value = [] | |
for name, col in zip(col_names, range(s.ncols)): | |
record = {} | |
title = s.cell(row,4).value | |
description = s.cell(row,5).value | |
record['title'] = title | |
record['description'] = description | |
self.collection4.insert(record) | |
article_write = open('new_training_data/suspected/new_tagged_document_'+str(row), 'w') | |
article_write.write(u'{}\n\n{}'.format(title , description).lower().encode('utf-8').strip()+'\n') | |
article_write.close() | |
break | |
#Function to write file | |
article_write = open('unsuspected/new_untagged_document_'+str(i), 'w') | |
article_write.write(u'{}\n\n{}'.format(record['title'], record['description']).lower().encode('utf-8').strip()+'\n' | |
article_write.close() | |
#Function to read and write pickle file | |
import pickle | |
X_train = self.hasher.fit_transform(raw_X) | |
readx = open('x_result.pickle', 'wb') | |
pickle.dump(X_train, readx) | |
readx.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment