私はxlrd
xlsファイルで作業するために使用しています。私の xls ファイルには 2 つの列があり、両方の列の行数が同じであることを確認する必要があります。から、インデックスで指定された行の長さを探すhelp()
必要があることを学びましたが、 を見つけることができません。何かお手伝いできますかrow_len()
col_len
これが私のコードです
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"
以下の比較以外にいくつかのオプションがあります。
>>> print len(sheet1.col_values(0))
8
>>> print len(sheet1.col_values(1))
8
@alecxe さん、返信ありがとうございます。コードにさらに数行を追加する代わりに、以下のことがわかりました。これがうまくいくかどうかアドバイスしてください
>>> 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
>>>