我还需要在迭代器循环内部推进从excel和excel电子表格中读取的行的迭代。
try:
for r in range(insheet.nrows):
cdat = insheet.cell(r, 0).value
if not cdat == xlrd.empty_cell.value:
if hasNumbers(cdat):
#Strip digits to get Client pneumonic
#pdb.set_trace()
if re.sub(r'\d','',cdat) == CLNTID:
#pdb.set_trace()
#This call belongs to this client
memId = cdat
fInCallBlock = True
while fInCallBlock:
#Output this line formatted as CallHeader
#Inc row counter
Can't do this ==> next(r)
cdat = cdat.strip(insheet.cell(r, 0).value)
if cdat == u"Call Resolved":
pdb.set_trace()
fInCallBlock = False
代码崩溃,标记为:
-> next(r)
(Pdb++) n
TypeError: 'int object is not an iterator'
我无法找到如何做到这一点。
答案 0 :(得分:0)
您可以将range
分配给变量,然后将其前进
myrange = iter(range(insheet.rows))
for r in myrange:
...
next(myrange)
当然range
在Python 3中以这种方式工作。如果仍然在Python 2下,它将是xrange
请注意r
值不会改变,除非您不仅推进迭代器,还要将其输出分配给r
:
r = next(myrange)
您还应该推进StopIteration
range
例外情况
答案 1 :(得分:0)
r
是来自range()
的整数,而不是迭代器,因此您无法在其上调用next()
。
您可以使用带有计数器的while
循环而不是for
循环:
try:
r = 0
while r < insheet.nrows:
cdat = insheet.cell(r, 0).value
if not cdat == xlrd.empty_cell.value:
if hasNumbers(cdat):
#Strip digits to get Client pneumonic
#pdb.set_trace()
if re.sub(r'\d','',cdat) == CLNTID:
#pdb.set_trace()
#This call belongs to this client
memId = cdat
r += 1
while r < insheet.nrows:
#Output this line formatted as CallHeader
cdat = cdat.strip(insheet.cell(r, 0).value)
if cdat == u"Call Resolved":
break
#Inc row counter
r += 1
r += 1