I'm wondering how to use xlrd with python to find a specific word in an excel file and then output the sentence that contains that word.
Asked
Active
Viewed 558 times
1 Answers
0
Python in general has very good documentation on how to work with files and how to look for info in data structures.
xlrd has also very good documentation, so I definitely recommend checking that out.
Regarding your question, please have a look at Help section to see how to ask questions. It's generally welcome when you pass some code with the question.
For the answer, here is quick example:
import xlrd
try:
excel_workbook = xlrd.open_workbook("test.xls")
except Exception as e:
print e
sheet0 = excel_workbook.sheet_by_index(0)
new_file = open("test.file","w")
for row_idx in xrange(sheet0.nrows):
row = sheet0.row_values(row_idx)
if "Epic" in row:
new_file.write(",".join(str(value) for value in row))
new_file.write("\n")
new_file.close()
-
Thanks for the help, I appreciate it. But for some reason it only matches if you put the whole sentence in to look for. For example if i search for "Hot" it doesn't find it when the sentence "Hot Edge-Cold Core storage architecture with DSSD" exists in the file. – Haldamir Apr 08 '15 at 15:14
-
In this example I am iterating through rows in first sheet in excel file. Each row is represented as a list of string items, where each item is one column in row. So if you have sentences in columns then you will want to add checking on each column. – ThePavolC Apr 08 '15 at 15:22