2013-02-18 43 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()) 

我不知道你是什麼意思「在對象存儲」,但代碼很容易修改,以滿足您的目的。