如何解决" IndexError:字符串索引超出范围" python中的错误?

时间:2017-11-16 23:36:04

标签: python error-handling

我使用以下代码从文本文件中读取数据,并在某些行之间添加一些行,但在运行代码之后,在执行此操作之前,我收到了如下所述的错误。

文本文件中的数据是从csv文件转换而来的,并且是逗号分隔的。

import os
import re
import time
from datetime import datetime

file_list = []
file_counter = 1
for filename in os.listdir(os.getcwd()):
    file_list.append(filename)

for filename in file_list:
    if(filename=='q.py'):   continue
    file = open(filename,"r").read().split('\n')
    file_to_write = "file" +str(file_counter) +".txt"
    file_w = open(file_to_write,"w")
    file_w.write(file[0])
    file_w.write("\n")
    number_of_lines = sum(1 for _ in file)
    lis = []
    for i in range(number_of_lines):
        lis.append(file[i])
    modified_time = []
    for i in range(number_of_lines):
        line = lis[i].split(' ')

        if(line[0]=='name' or line[0]==''):
            continue;
        temp1 = ""
        temp1+=line[2][0]
        temp1+=line[2][1]
        temp1+=line[2][2]
        temp1+=line[2][3]
        temp1+=line[2][4]
        temp1+=line[2][5]
        temp1+='0'
        temp1+='0'
        line[2] = temp1
        try:
            modified_time.append(line[0]+' '+line[1]+' '+line[2]+' 
'+line[3]+' '+line[4]+' '+line[5])
        except:
            continue

    for i in range(len(modified_time)-1):
        line1 = modified_time[i].split(' ')
        if(line1[0]=='name' or line1[0]==''):
            continue;
        line2 = modified_time[i+1].split(' ')
        date1 = line1[1]
        date2 = line2[1]
        time1 = line1[2]
        time2 = line2[2]
        day1 = datetime.strptime(date1, '%Y/%m/%d').date()
        day2 = datetime.strptime(date2, '%Y/%m/%d').date()
        diff1 = (day2-day1).days*24*60


        format = '%H:%M:%S'
        startDateTime = datetime.strptime(time1, format)
        endDateTime = datetime.strptime(time2, format)

        diff2 = endDateTime-startDateTime
        diff2 = diff2.seconds/60

        diff =  diff1 + int(diff2)

        lis_written = line1[0] + ' ' + line1[1] + ' ' + line1[2] + ' ' + 
line1[3] + ' ' + line1[4] + ' ' + line1[5] + '\n';
        file_w.write(lis_written)
        format = '%Y/%m/%d %H:%M:%S'
        time_counter = datetime.strptime(line1[1]+' '+line1[2], format)


        from datetime import timedelta

        for i in range(diff-1):
            time_counter = time_counter +timedelta(0,60)
            time_value = str(time_counter)
            time_value = time_value.split(' ')
            giventime = time_value[1]
            givendate = time_value[0]

            temp_str = ""
            temp_str+=givendate[0]
            temp_str+=givendate[1]
            temp_str+=givendate[2]
            temp_str+=givendate[3]
            temp_str+='/'
            temp_str+=givendate[5]
            temp_str+=givendate[6]
            temp_str+='/'
            temp_str+=givendate[8]
            temp_str+=givendate[9]

            lis_written = line1[0] + ' ' + temp_str + ' ' + giventime + ' ' + 
str(-999) + ' ' + line1[4] + ' ' + line1[5] + '\n';
            file_w.write(lis_written)


    file_w.write(file[len(file)-1])
    file_w.write("\n")
    file_counter+=1

这是我在第33行得到的错误:

Traceback (most recent call last):
  File "C:\Users\bxr5813\Desktop\time series\run\New folder\New folder\q.py", 
line 33, in <module>
    temp1+=line[2][4]
IndexError: string index out of range

输入文本文件的格式如下:

Value Date Time MilliSecond
919 04/15/16 19:41:02 700682752
551 04/15/16 19:46:51 014109952
717 04/15/16 19:49:48 333956864
2679 04/15/16 19:52:49 8053952
2890 04/15/16 19:55:43 73351552
2897 04/15/16 19:58:38 257767936
1790 04/15/16 21:39:14 13785728
2953 04/15/16 21:42:10 801841152
2516 04/15/16 21:45:04 467205376
2530 04/15/16 21:47:58 688858368
2951 04/15/16 21:51:02 6165952
2954 04/15/16 21:53:56 48836992
2537 04/15/16 21:56:52 105879296
2523 04/15/16 21:59:45 920951808
2536 04/15/16 22:02:49 103219968
2727 04/15/16 22:05:43 708147456
2554 04/15/16 22:11:48 323045888
2703 04/15/16 22:14:46 932627712
2958 04/15/16 22:17:40 574788352
2683 04/15/16 22:20:34 7734976
2542 04/15/16 22:23:29 353888512
2536 04/15/16 22:29:15 787323136

2 个答案:

答案 0 :(得分:1)

你试过调试吗?我认为您应该将if(line[0]=='name' or line[0]==''):更改为if(line[0]=='Value' or line[0]==''):并获得Index Error,因为代码会尝试在第一个循环中复制'Time'的第四个索引。

此外,您的代码似乎需要一些简化。例如,您可以写

file_list = []
for filename in os.listdir(os.getcwd()):
    file_list.append(filename)

file_list = [i for i in os.listdir(os.getcwd())]

您还可以简化

temp1 = ""
temp1+=line[2][0]
temp1+=line[2][1]
temp1+=line[2][2]
temp1+=line[2][3]
temp1+=line[2][4]
temp1+=line[2][5]
temp1+='0'
temp1+='0'
line[2] = temp1

line[2] = line[2][:5] + '00'

line[0]+' '+line[1]+' '+line[2]+' '+line[3]+' '+line[4]+' '+line[5] + '\n'可以修改为' '.join(modified_time) + '\n'line数组似乎有索引3为最大值,而不是5。

此外,如果您不再使用文件,建议您使用file_used.close()关闭已打开以进行读取或写入的文件。

我没有在整个程序中进行调试,但我认为您需要调试代码。我建议您使用Visual Studio Code程序。这非常有用。

答案 1 :(得分:0)

如果你想抓住第3行(索引2)的最后一个元素,它将是

  

temp1 + = line [2] [3]

引用行[x] [4]意味着你想要一行只有4

的第5列