Skip to content

Instantly share code, notes, and snippets.

@karimkhanp
Last active September 7, 2016 06:40
Show Gist options
  • Save karimkhanp/c39e6da3159c85fb2352db7389198399 to your computer and use it in GitHub Desktop.
Save karimkhanp/c39e6da3159c85fb2352db7389198399 to your computer and use it in GitHub Desktop.
Useful python function
#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