Python仅在文本文件中的某些位置执行操作

时间:2015-02-06 21:32:39

标签: python regex parsing

我有一个包含这样的数据的文本文件

AA 331             
line1 ...   
line2 ...    
% information here     
AA 332   
line1 ...    
line2 ...    
line3 ...   
%information here    
AA 1021   
line1 ...   
line2 ...  
% information here      
AA 1022    
line1 ...   
% information here     
AA 1023    
line1 ...    
line2 ...    
% information here    

我想仅针对位于行"AA 331"和行"AA 1021"之后的最小整数之后的“信息”执行操作,而不是在行"AA 332""AA 1022"之后执行操作"AA 1023"

P.s这只是大文件的示例数据

下面的代码我尝试解析文本文件并获取列表“list1”中“AA”之后的整数,并在第二个函数中将它们分组以获得“list2”中的最小值。这将返回[331,1021,...]之类的整数。所以我想要提取“AA 331”之后的行并执行动作,但我不知道如何继续。

from itertools import groupby
def getlineindex(textfile):
    with open(textfile) as infile:
    list1 = []
    for line in infile :
        if line.startswith("AA"):
            intid = line[3:]
            list1.append(intid)
    return list1

def minimalinteger(list1):
     list2 = []
     for k,v in groupby(list1,key=lambda x: x//10):
           minimalint = min(v)
           list2.append(minimalint)
     return list2

list2包含“AA”之后的最小整数[331,1021,..]

2 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

import re

matcher = re.compile("AA ([\d]+)")
already_was = []
good_block = False

with open(filename) as f:
   for line in f:
        m = matcher.match(line)
        if m:
           v = int(m.groups(0)) / 10
        else:
           v = None

        if m and v not in already_was:
            good_block = True
            already_was.append(m)
        if m and v in already_was:
            good_block = False
        if not m and good_block:
            do_action()

仅当组中的第一个值为最小值时,这些代码才有效。

答案 1 :(得分:1)

好的,这是我的解决方案。在高级别,我逐行,看着AA线知道我何时找到了数据块的开始/结束,并观察我所谓的运行编号,以了解我们是否应该处理下一个块。然后,我有一个子程序来处理任何给定的块,基本上读取所有相关的行并在需要时处理它们。该子程序用于监视 next AA线,以便知道它何时完成。

import re

runIdRegex = re.compile(r'AA (\d+)')

def processFile(fileHandle):
    lastNumber = None  # Last run number, necessary so we know if there's been a gap or if we're in a new block of ten.
    line = fileHandle.next()
    while line is not None:  # None is being used as a special value indicating we've hit the end of the file.
        processData = False
        match = runIdRegex.match(line)
        if match:
            runNumber = int(match.group(1))
            if lastNumber == None:
                # Startup/first iteration
                processData = True
            elif runNumber - lastNumber == 1:
                # Continuation, see if the tenths are the same.
                lastNumberTens = lastNumber / 10
                runNumberTens = runNumber / 10
                if lastNumberTens != runNumberTens:
                    processData = True
            else:
                processData = True

            # Always remember where we were.
            lastNumber = runNumber

            # And grab and process data.
            line = dataBlock(fileHandle, process=processData)
        else:
            try:
                line = fileHandle.next()
            except StopIteration:
                line = None

def dataBlock(fileHandle, process=False):
    runData = []
    try:
        line = fileHandle.next()
        match = runIdRegex.match(line)
        while not match:
            runData.append(line)
            line = fileHandle.next()
            match = runIdRegex.match(line)
    except StopIteration:
        # Hit end of file
        line = None

    if process:
        # Data processing call here
        # processData(runData)
        pass

    # Return line so we don't lose it!
    return line

给你一些笔记。首先,我同意Jimilian的意见,你应该使用正则表达式匹配AA线。

其次,我们在处理数据时所讨论的逻辑是在processFile中。特别是这些行:

        processData = False
        match = runIdRegex.match(line)
        if match:
            runNumber = int(match.group(1))
            if lastNumber == None:
                # Startup/first iteration
                processData = True
            elif runNumber - lastNumber == 1:
                # Continuation, see if the tenths are the same.
                lastNumberTens = lastNumber / 10
                runNumberTens = runNumber / 10
                if lastNumberTens != runNumberTens:
                    processData = True
            else:
                processData = True

我认为我们不想处理数据,然后确定我们何时这样做。从逻辑上讲,您可以执行与此相反的操作,并假设您要处理数据,然后确定何时不处理数据。接下来,我们需要存储 last 运行的值,以便了解我们是否需要处理此运行的数据。 (并注意第一次运行边缘情况)我们知道我们想要在序列被破坏时处理数据(两次运行之间的差异大于1),这由else语句处理。我们也知道我们想要在序列递增数十位的数字时处理数据,这是由我的整数除以10来处理的。

第三,注意dataBlock的返回数据。如果你不这样做,你将失去导致dataBlock停止迭代的AA线,并且processFile需要该线以便知道是否应该处理下一个数据块。

最后,我选择使用fileHandle.next()和异常处理来确定何时到达文件末尾。但不要以为这是唯一的方法。 :)

如果您有任何问题,请在评论中告诉我。