我正在编写一个 Python 程序来读取文本文件并提取一些信息。我试图找到三个项目,一个实数和两个列表。该脚本将文本文件的行存储为一个列表inLines
。通读脚本使用的行时for curLine in inLines:
,然后在所有行中搜索特定键。一旦我找到搜索键,我想将剩余部分传递给inLines
一个函数,再读几行,然后在函数停止的那一行返回到主脚本。
这是我想要发生的事情的小图(以注释形式给出的代码说明)
line of text that doesn't matter #Read by main, nothing interesting happens
line of text that doesn't matter #Read by main, nothing interesting happens
search key A #Read by main, all following lines passed to function A
line that matters #Read by function A, stores in object
line that matters #Read by function A, stores in object
line that matters #Read by function A, stores in object
search key B #Read by function A, return to main, all following lines passed to function B
line that matters #Read by function B, stores in object
search key C #Read by function B, return to main, all following lines passed to function C
line that matters #Red by function C, stores in object
所以每个搜索键都会告诉程序要在哪个函数中(并且不同的键可以按任何顺序排列)。当脚本找到键时,它将所有进一步的行传递给正确的函数,并且每当一个函数找到一个搜索键时,它就会中断,并将所有进一步的行传递回 main(然后将相同的行传递给适当的函数)
很抱歉这本书的问题,我只是在多年的 FORTRAN 之后学习 Python,所以如果有人能想到更好的方法来解决这个问题,我愿意接受建议。提前致谢