Python程序使用re

时间:2017-10-05 16:00:40

标签: python

我正在尝试编写一个小程序/类,我在文件中找到某个文本块。然后我从该块中提取某些信息并简化它并打印/返回它。

这:

it /'properties'/'someJenkinsInformation'/'strategy(class:hudson.LogRotator)'
{
'daysToKeep'('90')
'numToKeep'('300')
'artifactDaysToKeep'('3')
'artifactNumToKeep'('3')
}

对此:

logRotator(90, 300, 3, 3)

到目前为止我所拥有的:

# Search test.txt for 'LogRotator'
def find_text(self):
    super.find_text()
    self.convert_to_string()
    # now that we have found our line, find the next piece


# From find_text print 'logRotator(90, 300, 3, 3)'
def create_text():
    j = 0

    while self.file_text[line_num + j].strip() != "}":
        while self.file_text[line_num + j].strip() != ")":
            match = re.search(r"[0-9]+", self.file_text[line_num + j])
    # this is mostly where I get lost in 
    # how to iterate through the above block and how to pull out what I 
    # need in order to print

1 个答案:

答案 0 :(得分:1)

我并不完全清楚你要问的是什么。这可能是您需要的技术。

它涉及逐行解析,在' {'匹配'}时会遇到并退出看到了。

import re

processing = False
result = 'logRotator('
with open('temp.txt') as text:
    for line in text:
        if line.startswith('{'):
            processing = True
        elif line.startswith('}'):
            break
        else:
            if processing:
                m = re.search('\d+', line)
                if m:
                    result += '%s, '%m.group()
print (result[:-2]+')')

这是输出。

logRotator(90, 300, 3, 3)