我希望有人能指出我出錯的地方。我正在尋找迭代「mylist」列表以獲取第一個條目並將該第一個條目用作搜索字符串,然後執行搜索並在找到該字符串後收集特定信息並將其發佈到Excel工作表中。然後我希望迭代到下一個「mylist」條目並執行另一個搜索。第一次迭代執行正常,但環路我得到以下CMD窗口錯誤的第二次迭代...Python,遍歷列表來執行搜索
2014 Apr 25 09:43:42.080 INFORMATION FOR A
14.01
Traceback (most recent call last):
File "C:\TEST.py", line 362, in <module>
duta()
File "C:\TEST.py", line 128, in duta
if split[10] == 'A':
IndexError: list index out of range
Exception RuntimeError: RuntimeError('sys.meta_path must be a list of
import hooks',) in <bound method Workbook.__del__ of
<xlsxwriter.workbook.Workbook object at 0x0238C310>> ignored
這裏是我的代碼...
for root, subFolders, files in chain.from_iterable(os.walk(path) for path in paths):
for filename in files:
if filename.endswith('.txt'):
with open(os.path.join(root, filename), 'r') as fBMA:
searchlinesBMA = fBMA.readlines()
fBMA.close()
row_numBMAA+=1
num = 1
b = 1
print len(mylist)
print (mylist[num])
while b<len(mylist):
for i, line in enumerate(searchlinesBMA):
for word in [mylist[num]]:
if word in line:
keylineBMA = searchlinesBMA[i-2]
Rline = searchlinesBMA[i+10]
Rline = re.sub('[()]', '', Rline)
valueR = Rline.split()
split = keylineBMA.split()
if split[6] == 'A':
print keylineBMA
print valueR[3]
worksheetFILTERA.write(row_numBMAA,3,valueR[3], decimal_format)
row_numBMAA+=1
break
num+=1
b=+1
任何想法,我我做錯了嗎?我的循環是否偏離了位置,還是我沒有輸入正確的列表指針?
感謝, MikG
不'關閉'用'with'打開的文件只是向左縮進,它會自動關閉。爲什麼使用'num'和'b',它們看起來就像是一樣的東西。 – cmd