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

时间:2021-02-11 16:40:12

Hi I am new to python and struggling my way out. Currently ia m doing some appending excel files kind of task and here's my sample code. Getting list out of index error as according to me while loop is not breaking at rhe end of each excel file. Any help would be appreciated. Thanks:

嗨,我是蟒蛇新手,正在努力解决问题。目前我正在做一些附加excel文件的任务,这是我的示例代码。按照我的方式获取索引错误列表,而循环不会在每个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)

2 个解决方案

#1


0  

In place of while True use 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
Read more about while loop

因为虽然True意味着在循环中运行这个循环无限时间。(或者直到它意味着break关键字)。阅读更多关于while循环的信息

#2


0  

while True loop basically means: execute the following code block to infinity, unless a break or sys.exit statement get you out.

而True循环基本上意味着:执行以下代码块到无穷大,除非break或sys.exit语句让你出局。

So in your case, you need to terminate after the lines to append the excel are over (exhausted). You have two options: check if there are more lines to append, and if not break.

所以在你的情况下,你需要在追加excel的行结束(耗尽)之后终止。您有两个选择:检查是否有更多行要追加,如果没有中断。

A more suitable approach when writing a file is for loops. This kind of a loop terminates when it is exausted.

编写文件时更合适的方法是for循环。这种循环在它耗尽时终止。

Also, you should consider gathering the content of the excel in one operation, and save it to a variable. Then, once you have it, create iteration and append it to csv.

此外,您应该考虑在一个操作中收集excel的内容,并将其保存到变量中。然后,一旦拥有它,创建迭代并将其附加到csv。

#1


0  

In place of while True use 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
Read more about while loop

因为虽然True意味着在循环中运行这个循环无限时间。(或者直到它意味着break关键字)。阅读更多关于while循环的信息

#2


0  

while True loop basically means: execute the following code block to infinity, unless a break or sys.exit statement get you out.

而True循环基本上意味着:执行以下代码块到无穷大,除非break或sys.exit语句让你出局。

So in your case, you need to terminate after the lines to append the excel are over (exhausted). You have two options: check if there are more lines to append, and if not break.

所以在你的情况下,你需要在追加excel的行结束(耗尽)之后终止。您有两个选择:检查是否有更多行要追加,如果没有中断。

A more suitable approach when writing a file is for loops. This kind of a loop terminates when it is exausted.

编写文件时更合适的方法是for循环。这种循环在它耗尽时终止。

Also, you should consider gathering the content of the excel in one operation, and save it to a variable. Then, once you have it, create iteration and append it to csv.

此外,您应该考虑在一个操作中收集excel的内容,并将其保存到变量中。然后,一旦拥有它,创建迭代并将其附加到csv。