[英]Python : XLRD; compare the columns length
I'm using xlrd
to work on xls files. 我正在使用
xlrd
处理xls文件。 My xls file has got two columns and my requirement is to make sure both the columns have got equal number of rows. 我的xls文件有两列,我的要求是确保两列的行数相等。 I learnt from
help()
that we have got a row_len()
to look for the length of a row given with the index, but unable to find any for col_len
. 我从
help()
中了解到,我们有一个row_len()
来查找索引给出的行的长度,但无法找到任何col_len
。 Can you please help with any 你能帮忙吗?
Here is my code 这是我的代码
from xlrd import open_workbook
spread_sheet=open_workbook("simple.xls")
sheet1=spread_sheet.sheet_by_index(0)
#validates the no of columns in the Spread sheet
if sheet1.ncols == 2:
for sheet1_rows in range(sheet1.nrows):
for sheet1_cols in range(sheet1.ncols):
value=sheet1.cell(sheet1_rows,sheet1_cols).value
source=sheet1.cell(sheet1_rows,0).value
destination=sheet1.cell(sheet1_rows,1).value
#ignores the Source and Destination Headers
if value not in ('Source','Destination'):
print "Source is : %s \nDestination is : %s\n" % (source,destination)
else:
print "XLS provided is not valid. Check the no of columns is 2"
Some other options apart from comparing the below please 除了比较以下之外,还有其他一些选择
>>> print len(sheet1.col_values(0))
8
>>> print len(sheet1.col_values(1))
8
Thanks for your reply @alecxe. 谢谢你的回复@alecxe。 Instead adding few more lines to my code, I found out something below.
而是在我的代码中添加更多行,我在下面找到了一些东西。 please advise will this work out
请告知这项工作
>>> print len(sheet1.col_values(0))
6
>>> print len(sheet1.col_values(1))
6
>>> sheet1.col_values(0)
[u'A', 1.0, 1.0, 1.0, 1.0, 2.0]
>>> sheet1.col_values(1)
[u'B', 2.0, 2.0, 2.0, 2.0, '']
>>> print len(filter(None,sheet1.col_values(1)))
5
>>>
You can't use len(sheet.col_values(index))
for measuring how many cells are set in the column (column length). 您不能使用
len(sheet.col_values(index))
来测量列中设置的单元格数(列长度)。 col_values
length is always equal to sheet.nrows
. col_values
长度始终等于sheet.nrows
。
Imagine you have the following in the input.xls
: 想象一下,你在
input.xls
有以下input.xls
:
A B
1 2
1 2
1 2
1 2
2
Then len(sheet.col_values(0))
will return 5 (as well as len(sheet.col_values(1))
), which is incorrect. 然后
len(sheet.col_values(0))
将返回5(以及len(sheet.col_values(1))
),这是不正确的。 Should be 4. 应该是4。
Instead, it's better to use something like this: 相反,最好使用这样的东西:
from itertools import takewhile
import xlrd
def column_len(sheet, index):
col_values = sheet.col_values(index)
col_len = len(col_values)
for _ in takewhile(lambda x: not x, reversed(col_values)):
col_len -= 1
return col_len
book = xlrd.open_workbook("input.xls")
sheet = book.sheet_by_index(0)
print column_len(sheet, 0) # prints 4
print column_len(sheet, 1) # prints 5
Hope that helps. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.