在Python for循环中查阅以前的索引

在Python for循环中查阅以前的索引,python,Python,我有一个脚本可以在文本中搜索关键字(例如:APPLE),如果为true,它会将同一行中同一文本的另一部分(例如:“00”或“01”或“02”)更改为“22”,作为示例,我将遵循以下三行: 输入: 123456香蕉00 SP 123457苹果01 RJ 123458梨02毫克 脚本运行 输出: 123456香蕉00 SP 123457苹果22RJ 123458梨02毫克 这三行是同一“标题”的一部分 已经这样做的代码(位置的数量并不反映实际情况,这只是一个示例,但在实际文件中,它们有一个位置固定的

我有一个脚本可以在文本中搜索关键字(例如:APPLE),如果为true,它会将同一行中同一文本的另一部分(例如:“00”或“01”或“02”)更改为“22”,作为示例,我将遵循以下三行:

输入:
123456香蕉00 SP
123457苹果01 RJ
123458梨02毫克

脚本运行

输出:
123456香蕉00 SP
123457苹果22RJ
123458梨02毫克

这三行是同一“标题”的一部分

已经这样做的代码(位置的数量并不反映实际情况,这只是一个示例,但在实际文件中,它们有一个位置固定的布局):

然而,在这个解决方案中有一个逻辑问题,因为我的现实是,如果在第二行我找到了“苹果”,我将不会更改同一行,而是在顶行,它属于“香蕉”,并且我的代码已经通过。因此,如果脚本结束,我预期的结果是:

123456香蕉22SP
123457苹果01 RJ
123458梨02毫克

我想到的解决方案是:创建一个名为“退出”的列表并增加它,这样当我需要检索一个位置时,我只需执行一个[exit-1],但不起作用。。。在文件中只保存已更改的行,这是因为我用以前的行覆盖了当前的“行”,但我不知道如何继续。以下是我的解决方案不起作用:


import shutil, tempfile

# reads from file and writes to another temporary file
exit = []
with open ('teste.txt', 'r') as file, \
     tempfile.NamedTemporaryFile ('w', delete = False) as out:
    
    for i, line in enumerate (file):
        code = line [07:14]
        
        if code == 'APPLE':
           print ("FOUND, CODE:" + code)
           line = exit [i - 1]
           line = line [: 14] + "22" + line [16:] # reassemble the line
           
        else:
           print ("NOT FOUND, CODE:" + code)
           
        out.write (line) # write to temporary file
        exit.append (line)
        
# move the temporary file to the original
shutil.move (out.name, 'test.txt')
最后,我的问题是:在用python重新组装行之前,如何“返回”索引中的位置

更新

我遵循了一些建议,创建了一个包含所有行的列表,以便在必要时可以返回,所以我做到了:

import shutil, tempfile

outLines = []
# reads from file and writes to another temporary file
with open ('teste.txt', 'r') as file, \
     tempfile.NamedTemporaryFile ('w', delete = False) as out:
        for i, line in enumerate (file):
            exit.append (line)
            code = line [14:23]
            if code == 'APPLE':
               print ("FOUND, CODE:" + code)
               outLines [i] = outLines [i-1] [:14] + "22" + outLines [i-1] [16:] # reassemble the line
               lineout = outLines [i]
               out.write (lineout)
            else:
               print ("NOT FOUND, CODE:" + code)
               
            out.write (line) # write to temporary file
# move the temporary file to the original
shutil.move (out.name, 'test.txt')
现在的问题是我的代码重复了第一行,对于exmpl,我的输出现在是这样的:

123456香蕉01 SP
123456香蕉22SP
123457苹果01 RJ
123458梨02毫克


如何在不重复第一行的情况下继续?我想这是因为我稍后录制了“行”,而他们认为行没有改变…

你的问题是,你在遇到每一行时都要写下它。当您找到
APPLE
时,您已经将
BANANA
行写入了输出文件

对代码最直接的更改是将所有行保存在
exit
中,直到您读取了整个输入文件。当您找到
APPLE
时,请更改
exit
的上一个元素(请注意,您当前的代码无法执行此操作)。当到达输入文件的末尾时,从
exit
写入输出文件

注意:您应该更改变量名
exit
,因为这是内置函数的名称


你能从这里处理逻辑吗?

我在帖子中做了更新。。。你能帮我完成吗?我已经完成了:你没有使用我的“最直接的改变”建议。
import shutil, tempfile

outLines = []
# reads from file and writes to another temporary file
with open ('teste.txt', 'r') as file, \
     tempfile.NamedTemporaryFile ('w', delete = False) as out:
        for i, line in enumerate (file):
            exit.append (line)
            code = line [14:23]
            if code == 'APPLE':
               print ("FOUND, CODE:" + code)
               outLines [i] = outLines [i-1] [:14] + "22" + outLines [i-1] [16:] # reassemble the line
               lineout = outLines [i]
               out.write (lineout)
            else:
               print ("NOT FOUND, CODE:" + code)
               
            out.write (line) # write to temporary file
# move the temporary file to the original
shutil.move (out.name, 'test.txt')