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和合计的单元格的索引。如上例中的SET I为1,0,合计为5,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:“列表”对象没有属性“行”
答案 0 :(得分:2)
假设使用熊猫阅读后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')
答案 1 :(得分: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'))
答案 2 :(得分: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