列表索引超出范围,无法剥离用户inpu

2024-09-24 00:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新手,正在学习如何编写代码。我一直在编写一个代码,它定义了一个函数,它可以修剪用户给定的一组行,并将其传递给另一个调用它的python脚本。代码如下:

import re 
import sys


def calltrimport():
        f=open("temp1.txt","w")
        print "Enter the complete call trace.\nMake Sure there are no extra or unnecessary white spaces in the content.\nPress Enter twice to finish input"
        file_lines = iter(raw_input, '') # Get lines as input until an empty line.
        file_content = '\n'.join(file_lines) # Join the file lines to one string, seperated by new-line.
        f.write(file_content)
        f.close()

        num_lines = 0
        with open("temp1.txt", 'r') as f:
                for line in f:
                        num_lines += 1

        print("Number of lines recorded in the entered text: ")
        print(num_lines)

        with open("temp1.txt","r") as f1:
                f2=open("temp2.txt","w")
                for content in range(0,num_lines):
                        try:
                                content = f1.readline()
                                sp = content.split('+')[0]
                                sp1 = sp.split('] ')[1]
                                sp1 = sp1.strip()
                                f2=open("temp2.txt","a")
                                f2.write(sp1+'\n')
                                f2.close
                        except IndexError, e:
                                print e
                                print "line = ", line
        with open('temp2.txt', 'r') as myfile:
                data=myfile.read().replace('\n', '')
        return data

我现在还将输入一个样本和预期输出:

^{pr2}$

文件“temp2.txt”中的预期输出如下所示:

garbage1
garbage2
garbage3
garbage4
garbage5
garbage6
garbage7
garbage7
garbage8

相反,我得到以下输出:

Number of lines recorded in the entered call trace: 
9
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]
list index out of range
line = [<ffffffffXXXXXXXX>] ? garbage1_+0x20e/0x760 [Nope]

你知道我在哪里调用超出循环边界的索引吗?我在这件事上浪费了很多时间,我真的很感激你的帮助。在

编辑: 按照Klaus的建议,将raise添加到except块后,我可以看到以下回溯:

Number of lines recorded in the entered call trace: 
9
list index out of range
line =  [<ffffffffXXXXXXXX>] ? some_garbage+0x20e/0x760 [Nope]
Traceback (most recent call last):
  File "dts_check_final.py", line 13, in <module>
    mystr = ct_imp.calltrimport()
  File "/users/siddharath/dts/Final/ct_imp.py", line 26, in calltrimport
    sp1 = sp.split('] ')[1]
IndexError: list index out of range

注:“dts U检查_最终.py“是从python脚本中调用的_小鬼). 在


Tags: oftheintxtindexlinerangeopen
2条回答

不幸的是,这只是一个猜测(部分原因是当出现错误时,打印的是line而不是{}),但是您的输入中是否会有制表符而不是空格?失败

split('] ')[1]

表示字符串'] '不出现在该行的(该部分)。在

更一般的做法是:尝试重写它,使其完全不打开任何文件(从一开始,内存中就已经有了所有的字符串)。简化的数据流应该使调试更容易。在

在对这个问题失去理智两天后,我意识到我的原始输入是“非中断空格”而不是“正则空格”。当我将输入粘贴到msword中并启用格式化字符显示时,我意识到了这一点。为了解决这个问题,我通过添加以下内容显式地将默认Python编码设置为“UTF-8”:

import sys
# encoding=utf8

reload(sys)
sys.setdefaultencoding('utf8')

还有维奥拉!它很好用。在

谢谢你们的帮助。如果我提出问题的方式有点不完整,我很抱歉。在

相关问题 更多 >