使用python在工作表中查找单元格的索引值

问题描述 投票:0回答:3
Product Name
SET I Violations 
Rule 1   0 
Rule 2   5 
Rule 3   0
Total    5

SET II Violations 
Rule 1   2
Rule 2   1 
Rule 3   1 
Total    4

SET III Violations
Rule 1    0 
Rule 2    0 
Rule 3    2 
Total     2

我想找到一个包含SET I,SET II,SET III,SET IV和Total的单元格的索引..就像上面的例子中的SET I一样,总计为1,0总计...而且规则不固定他们可以增加或减少

我试过以下代码:

import xlrd
import xlwt
from xlwt import Workbook
wb = xlrd.open_workbook('test.xls')
sheet = wb.sheet_by_index(0)
for row_num in range(sheet.nrows):
    row_value = sheet.row_values(row_num)
    if row_value[0].startswith('SET') :
     print (row_value.row,row_value.column)

上面的代码给出以下错误:AttributeError:'list'对象没有属性'row'

python excel python-3.x pandas xlwt
3个回答
2
投票

假设在使用pandas阅读后df看起来像这样:

df=pd.read_excel(file)
print(df)

   Product        Name
0    SET I   Violations
1    Rule 1           0
2    Rule 2           5
3    Rule 3           0
4     Total           5
5    SET II  Violations
6    Rule 1           2
7    Rule 2           1
8    Rule 3           1
9     Total           4
10  SET III  Violations
11   Rule 1           0
12   Rule 2           0
13   Rule 3           2
14    Total           2

然后,您可以使用如下的series.str.startswith()并调用True行的索引。

df[df.Product.str.startswith('SET')].index
#Int64Index([0, 5, 10], dtype='int64')

0
投票

因为检查'Rule'和'Total'的值是在列索引0,你可以直接打印0,

import xlrd
wb = xlrd.open_workbook('test.xls')
sheet = wb.sheet_by_index(0)
for row_num in range(sheet.nrows):
    row_value = sheet.row_values(row_num)
    if row_value[0].startswith('SET') :
        print(row_value[0],'-(row,col): ',row_num,",0",sep='')
    if row_value[0].startswith('Total') :
        print(row_value[0],'-(row,col): ',row_num,",0",sep='')

  >>
SET I-(row,col): 1,0
Total-(row,col): 5,0
SET II-(row,col): 7,0
Total-(row,col): 11,0
SET III-(row,col): 13,0
Total-(row,col): 17,0

0
投票

你需要print row_num,因为if条件,列索引将为0

import xlrd
import xlwt
from xlwt import Workbook
wb = xlrd.open_workbook('test.xls')
sheet = wb.sheet_by_index(0)
for row_num in range(sheet.nrows):
    row_value = sheet.row_values(row_num)
    if row_value[0].startswith('SET') :
        #because of if condition column index will be 0
        print (row_num,0)

如果索引没有修复,那么你可以打印值为'SET'的元素的索引

if 'SET' in row_value:
    print(row_num, row_value.index('SET'))
© www.soinside.com 2019 - 2024. All rights reserved.