Tuesday, 15 May 2012

Search into excel file with python -



Search into excel file with python -

first of all, i'm python beginner apologize trivial question :d seek search *.xls file specific word using python (v 2.7)

short problem description/specification : 1. test.xls input file 2. target word 2, want exctract cell contains 2 , not sometihngs 2 (e.g. cell value = 2 -> right! cell value=2345-> wrong !!)

below code :

book = open_workbook('test.xls',on_demand=true) item = 2 row=-1 n=0 name in book.sheet_names(): if name.endswith('traceability matrix'): sheet = book.sheet_by_name(name) rowindex = -1 cell in sheet.col(1): # n=n+1 if item in cell.value: print "val ",cell.value print "row ",sheet.row(n) break if row != -1: cells = sheet.row(row) cell in cells: print">>", cell.value book.unload_sheet(name)

now, output list of rows contains not 2 (see wrong case above, point 2), see below "print" results:

row [text:u'srs5617\nsrs5618\nsrs5619\nsrs5620', text:u'rq - 5282', empty:'', text:u'function plus', text:u'see note ', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'code inspection', text:u'(**), 2']

someone can help me? suggestion?

thanksssss !!!!!!!!

your problem on lines

if item in cell.value: print "val ",cell.value print "row ",sheet.row(n) break

that search if "2" is on cell.value, not if it is 2.

it changed if int(cell.value) == int(item):

python excel python-2.7

No comments:

Post a Comment