我正在编写一个Python程序来读取文本文件并提取一些信息。我试图找到三个项目,一个实数和两个列表。该脚本将文本文件的行存储为列表inLines
。在阅读脚本时,脚本使用for curLine in inLines:
,然后在所有行中搜索特定的键。找到搜索关键字后,我想将inLines
的剩余部分传递给一个函数,再读几行,然后返回到函数停止行的主脚本。将部分列表传递给Python函数
这里是什么,我希望发生的事情(给出意见代码指令)
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
所以每个搜索键告诉其功能是在程序(和不同的密钥可以以任意顺序一个小图)当脚本找到该键时,它将所有更多的行传递给正确的函数,并且每当函数找到一个搜索键时,它就会中断,并将所有其他行传回主(然后将相同的行传递给相应的函数)
对不起,这本书的一个问题,我刚刚学习了多年FORTRAN之后的Python,所以如果任何人都能想到更好的方法来解决这个问题,我愿意提供建议。在此先感谢
你可以传递一个文件句柄到一个函数,然后在该函数中继续'for l in inf'(虽然我现在不能写一个完整的答案) – 2013-02-18 20:54:52