簡體   English   中英

列表索引超出范圍錯誤打破python中的whiloe循環

[英]List index out of range error in breaking whiloe loop in python

嗨,我是python的新手,正在努力擺脫困境。 目前,我正在執行一些附加excel文件的任務,這是我的示例代碼。 根據我的說法,從列表錯誤中獲取列表錯誤而while循環在每個excel文件的末尾都沒有中斷。 任何幫助,將不勝感激。 謝謝:

import xlrd
import glob
import os
import openpyxl
import csv
from xlrd import open_workbook
from os import listdir
row = {}
basedir = '../files/'
files = listdir('../files')
sheets = [filename for filename in files if filename.endswith("xlsx")]
header_is_written = False

for filename in sheets:

print('Parsing {0}{1}\r'.format(basedir,filename))

worksheet = open_workbook(basedir+filename).sheet_by_index(0)


print (worksheet.cell_value(5,6))
counter = 0
while True:
    row['plan name'] = worksheet.cell_value(1+counter,1).strip()
    row_values = worksheet.row_slice(counter+1,start_colx=0, end_colx=30)

    row['Dealer'] = int(row_values[0].value)
    row['Name'] = str(row_values[1].value)
    row['City'] = str(row_values[2].value)
    row['State'] = str(row_values[3].value)
    row['Zip Code'] = int(row_values[4].value)
    row['Region'] = str(row_values[5].value)
    row['AOM'] = str(row_values[6].value)
    row['FTS Short Name'] = str(row_values[7].value)
    row['Overall Score'] = float(row_values[8].value)
    row['Overall Rank'] = int(row_values[9].value)
    row['Count of Ros'] = int(row_values[10].value)
    row['Count of PTSS Cases'] = int(row_values[11].value)
    row['% of PTSS cases'] = float(row_values[12].value)
    row['Rank of Cases'] = int(row_values[13].value)
    row['% of Not Prepared'] = float(row_values[14].value)
    row['Rank of Not Prepared'] = int(row_values[15].value)
    row['FFVt Pre Qrt'] = float(row_values[16].value)
    row['Rank of FFVt'] = int(row_values[17].value)
    row['CSI Pre Qrt'] = int(row_values[18].value)
    row['Rank of CSI'] = int(row_values[19].value)
    row['FFVC Pre Qrt'] = float(row_values[20].value)
    row['Rank of FFVc'] = int(row_values[21].value)
    row['OnSite'] = str(row_values[22].value)
    row['% of Onsite'] = str(row_values[23].value)
    row['Not Prepared'] = int(row_values[24].value)
    row['Open'] = str(row_values[25].value)
    row['Cost per Vin Pre Qrt'] = float(row_values[26].value)
    row['Damages per Visit Pre Qrt'] = float(row_values[27].value)
    row['Claim Sub time pre Qrt'] = str(row_values[28].value)
    row['Warranty Index Pre Qrt'] = str(row_values[29].value)
    counter += 1
    if row['plan name'] is None:
        break
    with open('table.csv', 'a',newline='') as f:
        w=csv.DictWriter(f, row.keys())
        if header_is_written is False:
            w.writeheader()
            header_is_written = True
        w.writerow(row)

代替while True for

row['plan name'] = worksheet.cell_value(1 + counter, 1).strip()
row_values = worksheet.row_slice(counter + 1, start_colx=0, end_colx=30)
for values in row_values:
    row['Dealer'] = int(values.value)
    row['Name'] = str(values.value)
    ....

因為while True表示在無限時間內運行此循環(或直到它表示break關鍵字)。
閱讀有關while循環的更多信息

while True循環基本上意味着:執行以下代碼塊至無窮大,除非breaksys.exit語句使您sys.exit

因此,在您的情況下,您需要在附加excel的行結束(用盡)后終止。 您有兩個選擇:檢查是否有更多行要追加,如果沒有break

寫文件時,更合適的做法是for循環。 這種循環在被欣賞時終止。

另外,您應該考慮通過一次操作收集excel的內容 ,並將其保存到變量中。 然后,一旦有了它,就創建迭代並將其附加到csv。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM