使用XLRD验证单元格值 [英] Validating a cell value using XLRD

查看:266
本文介绍了使用XLRD验证单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用XLRD包读取第2行(excel中的第1行)的值,特别是列A。



我知道值是text:u'NULL,但是如果我尝试编写一个if函数来比较一个给定相同值的变量,它不会将它们认为是一样的。



(只是为了解释我的电子表格在我所指的单元格中具有 Null 的价值,但使用XLRD将其读为text:u 'NULL'这是很好的)



我已经创建了一个我所指的是一个测试例子,因为这很难描述。我的代码第20行的b的值绝对是text:u'NULL,所以我不知道为什么它不会读取等于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)

##成功测试
a = ['lewis' 'dylan']
b = a [0]
c ='lewis'
如果c == b:
printc等于b
else:
打印失败

##测试失败
a =行
b = a [0]
c =text:u'NULL
如果c == b:
打印c等于b
其他:
打印失败在此输入代码


解决方案

上述两个值不相等,因为它们的类型不同。 >

假设我有这个表:

 名称|年龄| 
|约翰22 |
|菲尔| 25 |

现在我想读取单元格(2,1)。

>>> cell1 = sheet.cell(1,0)

>>> print cell1

text:u'John'



>>> match_string =text:u'John



现在 match_string cell1 出现相同。我们检查一下它们是否相等。



>>> cell1 == match_string

False



他们不是等于。我们检查一下它们是否相同:



>>> isinstance(cell1,basestring)

False

>> ;> type(cell1)

< class'xlrd.sheet.Cell'>



他们有不同的类型,所以我们无法比较。



解决方案:我们可以使用单元格的value属性进行比较



>>> cell1_value = cell1.value

>>>打印cell1_value

u'John'



这是的字符串类型,现在可以用来与字符串类型进行比较。



>>> cell1_value == u'John'

True



所以在你的情况下,你可以做:

>>>行[0] .value == u'Null'

True



此方法也可用于比较数字或检查任何条件。


I am trying to use the package XLRD to read the value of row 2(row 1 in excel), in particular column A.

I know that the value is "text:u'NULL'" but if I try to write an if function which compares a variable which I have given the same value, it does not recognise them as the same.

(Just to explain my spreadsheet has the value Null in the cell I am referring to but using XLRD it reads this as "text:u'NULL'" which is fine)

I have created a test example of what I am referring to as this is hard to describe. The value for b on line 20 of my code is definitely "text:u'NULL'", so I have no idea why it does not read this as equal to 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
a = ['lewis','dylan']
b = a[0]
c = 'lewis'
if c == b:
    print "c is equal to b"
else:
    print "fail"

## test that fails
a = row
b = a[0]
c = "text:u'NULL'"
if c == b:
    print "c is equal to b"
else:
    print "fail"enter code here

解决方案

The above two values are not equal because they are of different types.

Let's say I have this table:

| Name | Age |   
| John | 22  |  
| Phil | 25  |

Now I want to read the cell (2,1).
>>> cell1 = sheet.cell(1,0)
>>> print cell1
text:u'John'

>>> match_string = "text:u'John'"

Now match_string and cell1 appear same. Let's check if they are equal.

>>> cell1 == match_string
False

They are not equal. Let's check if they are of the same type:

>>> isinstance(cell1, basestring)
False
>>> type(cell1)
<class 'xlrd.sheet.Cell'>

They are of different types so we just can't compare them.

Solution: We can compare using the value attribute of a cell.

>>> cell1_value = cell1.value
>>> print cell1_value
u'John'

This is of the type string which now can be used to compare with string type.

>>> cell1_value==u'John'
True

So in your case, you can do:
>>> row[0].value == u'Null'
True

This method can also be used to compare for numbers or to check for any condition.

这篇关于使用XLRD验证单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆