i trying use package xlrd read value of row 2(row 1 in excel), in particular column a.
i know value "text:u'null'" if try write if function compares variable have given same value, not recognise them same.
(just explain spreadsheet has value null in cell referring using xlrd reads "text:u'null'" fine)
i have created test example of referring hard describe. value b on line 20 of code "text:u'null'", have no idea why not read equal b.
import xlrd book = xlrd.open_workbook("excelscores.xls") sheet_name = book.sheet_names()[0] sheet = book.sheet_by_name(sheet_name) row_no = 1 row = sheet.row(row_no) ## successful test = ['lewis','dylan'] b = a[0] c = 'lewis' if c == b: print "c equal b" else: print "fail" ## test fails = row b = a[0] c = "text:u'null'" if c == b: print "c equal b" else: print "fail"enter code here
the above 2 values not equal because they of different types.
let's have table:
| name | age | | john | 22 | | phil | 25 |
now want read cell (2,1).
>>> cell1 = sheet.cell(1,0)
>>> print cell1
text:u'john'
>>> match_string = "text:u'john'"
now match_string
, cell1
appear same. let's check if equal.
>>> cell1 == match_string
false
they not equal. let's check if of same type:
>>> isinstance(cell1, basestring)
false
>>> type(cell1)
<class 'xlrd.sheet.cell'>
they of different types can't compare them.
solution: can compare using value attribute of cell.
>>> cell1_value = cell1.value
>>> print cell1_value
u'john'
this of type string can used compare string type.
>>> cell1_value==u'john'
true
so in case, can do:
>>> row[0].value == u'null'
true
this method can used compare numbers or check condition.