正在寻找避免“列表索引超出范围错误”的方法

2024-09-19 23:27:44 发布

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

我制作了一个脚本,它可以工作,但前提是我知道locations.txt将有多少行。 有没有办法让脚本在不考虑行数的情况下工作?(我预计最多1到12行)

locline1 = content[1]
locline2 = content[2]

当位置列表中有两行时效果非常好。如果只有一个位置,那么我需要将脚本更改为

locline1 = content[1]
locline2 = content[1]

该解决方案避免了任何错误并允许其运行,提供了重复的结果,这比什么都没有要好

我试图制作另一个脚本,检查有多少行,并对第一个脚本进行适当的替换。它运行时没有出现错误,但没有进行替换,如果这是一条合适的路线,我可以稍后进行调查

正体

import varilen
exec(open("varilen.py").read())

下面是Varilen.py

file = open("locations.txt","r")
Counter = 1
Content = file.read()
CoList = Content.split("\n")

for i in CoList:
 if i:
  Counter += 1
  if Counter < 2:
   cr1test = open('amazing727p6.py', mode='w', encoding='UTF-8') 
   cr1 = (re.sub(r'content[2]', 'content[1]', cr1test, count=0, flags = re.DOTALL))

关于主脚本的更多细节(以防万一会有所不同)

该脚本读取一个基本文件,在其中查找位置的名称,并在新文件locations.txt的行中写入每个位置的一个副本

稍后,脚本读取locations.txt的行,并将这些行分配给这些行。 locline1=内容[1] locline2=内容[2]

进行替换,除去每行上的位置名称以外的所有内容。 将一个新文件另存为该文件(location name+_results.txt),然后在基础文件中搜索包含该位置名称的行,并将这些行写入该位置的results.txt

比如,; 搜索此基础文件

abc!New york aaaaaa a
gg3aa!New York aa bbbb
g44!Chicago au4s a3e

makelocations.txt

"New York
"Chicago

使“newyorkresults.txt”从基本文件复制匹配行并格式化列

abc!    New york aaaaaa a
gg3!aa  New York aa bbbb

这一切都正常,但如果locline12=content[12],locations.txt中没有第12行,那么它就根本不起作用

不要问具体的答案,尽管这总是受欢迎的。我只是想问,我应该如何着手修复/改进与此问题相关的脚本

***

EDIT:

***

这将提供更多细节, 这是代码在行前抛出的错误

rmuser4 = Path('lpgetandremoveuserresults1.txt', encoding='UTF-8').read_text()
rmuser5 = open('locations.txt', mode='w+')
rmuser6 = (re.sub(r'\[H.+', '', rmuser4))
print(rmuser6, file= rmuser5)
rmuser5.close()

lines_seen = set()

with open("locations.txt", "r+", encoding='UTF-8') as fqqw:
    dqqw = fqqw.readlines()
    fqqw.seek(0)
    for iqqw in dqqw:
        if iqqw not in lines_seen:
            fqqw.write(iqqw)
            lines_seen.add(iqqw)
    fqqw.truncate()
fqqw.close()

fileyu = open('locations.txt', encoding='UTF-8')
content = fileyu.readlines()


locline1 = content[1]
locline2 = content[2]
locline3 = content[3]
locline4 = content[4]
locline5 = content[5]
locline6 = content[6]
locline7 = content[7]
locline8 = content[8]
locline9 = content[9]
locline10 = content[10]
locline11 = content[11]
locline12 = content[12]

因此,它生成locations.txt文档,根据所使用的基本文件,它可能只有1行或12行

下面是后面代码的一部分,本帖中只显示了使用locline5/content[5]的代码,但脚本的代码从1到12几乎完全相同,而不仅仅是5

filenameline5 = (re.sub(r'"(.+?Events).+', '\\1', locline5, count=0, flags = re.DOTALL))
loc5new = open('%s results.txt' % filenameline5, mode='w+', encoding='UTF-8') 
print(filenameline5, file= loc5new)
loc5new.close()

filei5 = Path('%s results.txt' % filenameline5, mode='w+').read_text()

addq5rep = (re.sub(r'(.+?) .+', '"\\1', filei5, count=1, flags = re.A))
addq5base = open('search for %s.txt' % filenameline5, mode='w+', encoding='UTF-8')

print(addq5rep, file= addq5base)
addq5base.close()

list_file = open('search for %s.txt' % filenameline5)
search_words = []
for word in list_file:
    search_words.append(word.strip())
list_file.close()

matches = []

master_file = open('readable_export.txt', encoding='UTF-8')
for line in master_file:    
    current_line = line.split()    
    for search_word in search_words:       
        if search_word in current_line:           
            matches.append(line)            
            break
master_file.close()
new_file = open('%s results.txt' % filenameline5, 'w+')
for line in matches:
    new_file.write(line)
new_file.close()

所以问题是,如果locations.txt只创建了3行,那么后面的代码试图使用locline4到locline12将破坏一切。但是像这样更改代码可以修复它(如果locations.txt有3行)

locline1 = content[1]
locline2 = content[2]
locline3 = content[3]
locline4 = content[1]
locline5 = content[1]
locline6 = content[1]
locline7 = content[1]
locline8 = content[1]
locline9 = content[1]
locline10 = content[1]
locline11 = content[1]
locline12 = content[1]

我正在尝试修复它,这样我就不需要根据预期位置的行数手动进行这些编辑

多谢各位


Tags: 文件inretxt脚本forclosesearch