简体   繁体   English

列表索引超出范围错误打破python中的whiloe循环

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

Hi I am new to python and struggling my way out. 嗨,我是python的新手,正在努力摆脱困境。 Currently ia m doing some appending excel files kind of task and here's my sample code. 目前,我正在执行一些附加excel文件的任务,这是我的示例代码。 Getting list out of index error as according to me while loop is not breaking at rhe end of each excel file. 根据我的说法,从列表错误中获取列表错误而while循环在每个excel文件的末尾都没有中断。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks: 谢谢:

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)

In place of while True use for . 代替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)
    ....

because while True means to run this loop infinite time.(or until it means break keyword) inside while loop 因为while True表示在无限时间内运行此循环(或直到它表示break关键字)。
Read more about while loop 阅读有关while循环的更多信息

while True loop basically means: execute the following code block to infinity, unless a break or sys.exit statement get you out. while True循环基本上意味着:执行以下代码块至无穷大,除非breaksys.exit语句使您sys.exit

So in your case, you need to terminate after the lines to append the excel are over (exhausted). 因此,在您的情况下,您需要在附加excel的行结束(用尽)后终止。 You have two options: check if there are more lines to append, and if not break . 您有两个选择:检查是否有更多行要追加,如果没有break

A more suitable approach when writing a file is for loops. 写文件时,更合适的做法是for循环。 This kind of a loop terminates when it is exausted. 这种循环在被欣赏时终止。

Also, you should consider gathering the content of the excel in one operation, and save it to a variable. 另外,您应该考虑通过一次操作收集excel的内容 ,并将其保存到变量中。 Then, once you have it, create iteration and append it to csv. 然后,一旦有了它,就创建迭代并将其附加到csv。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM