蟒蛇:XLRD;比较列长度 [英] Python : XLRD; compare the columns length
问题描述
我正在使用 xlrd
处理 xls 文件.我的 xls 文件有两列,我的要求是确保两列的行数相同.我从 help()
了解到我们有一个 row_len()
来查找给定索引的行的长度,但找不到 col_len
.你能帮忙吗
这是我的代码
from xlrd import open_workbookspread_sheet=open_workbook("simple.xls")sheet1=spread_sheet.sheet_by_index(0)#验证电子表格中的列数如果 sheet1.ncols == 2:对于范围内的 sheet1_rows(sheet1.nrows):对于范围内的 sheet1_cols(sheet1.ncols):value=sheet1.cell(sheet1_rows,sheet1_cols).valuesource=sheet1.cell(sheet1_rows,0).value目的地=sheet1.cell(sheet1_rows,1).value#ignores 源头和目标头如果值不在 ('Source','Destination') 中:打印来源是:%s\n目的地是:%s\n"%(来源,目的地)别的:打印提供的 XLS 无效.检查列数是否为 2"
除了比较下面的一些其他选项,请
<预><代码>>>>打印 len(sheet1.col_values(0))8>>>打印 len(sheet1.col_values(1))8<小时>
感谢您的回复@alecxe.而不是在我的代码中添加了几行,我在下面找到了一些东西.请告知这是否可行
>>>打印 len(sheet1.col_values(0))6>>>打印 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, '']>>>打印 len(filter(None,sheet1.col_values(1)))5>>>
你不能用 len(sheet.col_values(index))
来测量列(column长度).col_values
长度总是等于 sheet.nrows
.
假设您在 input.xls
中有以下内容:
A B1 21 21 21 22
然后 len(sheet.col_values(0))
将返回 5(以及 len(sheet.col_values(1))
),这是不正确的.应该是 4.
相反,最好使用这样的东西:
from itertools import takewhile导入 xlrddef column_len(工作表,索引):col_values = sheet.col_values(index)col_len = len(col_values)for _ in takewhile(lambda x: not x, reversed(col_values)):col_len -= 1返回 col_lenbook = xlrd.open_workbook("input.xls")sheet = book.sheet_by_index(0)打印 column_len(sheet, 0) # 打印 4打印 column_len(sheet, 1) # 打印 5
希望有所帮助.
I'm using xlrd
to work on xls files. My xls file has got two columns and my requirement is to make sure both the columns have got equal number of rows. 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
. 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. 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). col_values
length is always equal to sheet.nrows
.
Imagine you have the following in the 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. Should be 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.
这篇关于蟒蛇:XLRD;比较列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!