2013-02-18 45 views
-1

我正在编写一个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,所以如果任何人都能想到更好的方法来解决这个问题,我愿意提供建议。在此先感谢

+0

你可以传递一个文件句柄到一个函数,然后在该函数中继续'for l in inf'(虽然我现在不能写一个完整的答案) – 2013-02-18 20:54:52

回答

1

这个小脚本是关闭到你想要的。它放弃在指定搜索功能之前发生的行。你应该能够适应你的需求。

import sys 


def search_func_a(l): 
    """ 
    Called for things that follow `search key A` 
    """ 
    print 'A: %s' % l 


def search_func_b(l): 
    """ 
    Called for things that follow `search key B` 
    """ 
    print 'B: %s' % l 


def search_key_func(l): 
    """ 
    Returns the associated "search function" for each search string. 
    """ 
    if 'search key A' in l: 
     return search_func_a 
    if 'search key B' in l: 
     return search_func_b 
    return None 


def main(): 
    # Start with a default handler. This changes as `search key` lines are 
    # found. 
    handler = lambda _: 0 

    for line in open('test.txt'): 
     # search_key_func returns None for non `search key` lines. In that 
     # case, continue to use the last `search function` found. 
     search_func = search_key_func(line) 
     if search_func: 
      # If a search line is found, don't pass it into the search func. 
      handler = search_func 
      continue 
     handler(line) 


if __name__ == '__main__': 
    sys.exit(main()) 
+0

我对Python有点新,所以你做的一些对我来说是全新的,但它看起来像我想要做的事情。但是这个处理器的功能是什么? – wnnmaw 2013-02-20 00:58:55

+0

处理程序是处理正在处理的当前行类型的函数。这是根据正在读取的当前行设置的。 – jaime 2013-02-22 13:39:49

0

是否有这样做的问题?

inA = inB = inC = False 
for line in file: 
    if keyA(line): 
    inA = True 
    inB = inC = False 
    elif keyB(line): 
    inB = True 
    inA = inC = False 
    elif keyC(line): 
    inC = True 
    inA = inB = False 
    if inA: 
    processA(line) 
    if inB: 
    processB(line) 
    if inC: 
    processC(line) 

你是问,如果有一些更快的方法?

+0

嗯,我已经重复了该文件,所以我不知道我是否可以在一个循环内循环,他们都在迭代相同的列表 – wnnmaw 2013-02-18 20:52:33

0
#!/usr/bin/env python3 
# encoding: utf-8 

import sys 

def find(haystack, needle, start_line = 0): 
    for i, line in enumerate(haystack[start_line:]): 
     if line == needle: 
      return i + 1 
    raise("%s not found." % needle) 

def main(argv = None): 
    if argv is None: 
     argv = sys.argv 

    with open(argv[1], 'r') as f: 
     text = f.read().splittext() 

    find(text, "c", find(text, "b", find(text, "a"))) 

if __name__ == "__main__": 
    sys.exit(main()) 

我不知道你是什么意思“在对象存储”,但代码很容易修改,以满足您的目的。