使用Python从文件中提取数据,如果模式匹配

2024-10-03 19:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在保存数据的文件中:

startTc:TC9

Client-1
IPAddress:10.203.205.111
Port:22
endTc:TC9

------------------------------------------------
startTc:TC5
Client-2
IPAddress of Client-2:10.203.205.112
Port:23
endTc:TC5
------------------------------------------------

如果startTc:TC5的条件匹配,则

Client-2
IPAddress of Client-2:10.203.205.112
Port:23

需要提取23英寸的端口: 文件读取需要在看到endTc:TC5时关闭


Tags: 文件of数据端口clientport条件ipaddress
2条回答

一种方法是使用regex,在下面的模式中,我使用positive look-around匹配startTc:TC5\n\nendTc:TC5之间的字符串,然后可以用\n分割结果:

>>> s="""startTc:TC9
... 
... Client-1
... IPAddress:10.203.205.111
... Port:22
... endTc:TC9
... 
...                         
... startTc:TC5
... Client-2
... IPAddress of Client-2:10.203.205.112
... Port:23
... endTc:TC5
...                         """
>>> re.search(r'(?<=startTc:TC5\n).*(?=\nendTc:TC5)',s,re.DOTALL).group(0).split('\n')
['Client-2', 'IPAddress of Client-2:10.203.205.112', 'Port:23']

请注意,如果要从文件中读取此字符串,则需要在re.search函数中使用open('file_name').read()而不是s

def getData(infilepath, start, end):
    with open(infilepath) as infile:
        data = []
        answer = []
        for line in infile:
            line = line.strip()
            if not line: continue
            if line == start or data:
                data.append(line)
            if line == end:
                temp = dict(data[1].split('-'))
                temp['ip'] = data[2].split(":")[1]
                temp['port'] = data[3].split(":")[1]
                answer.append(temp)
                data = []
    return answer

用法:

data = getData("path/to/file", "startTc:TC5", "endTc:TC5")
for d in data:
    print("Client:", d['Client'])
    print("IP:", d['ip'])
    print("Port:", d['port'])

相关问题 更多 >