簡體   English   中英

如果模式在Python中匹配,則從文件中提取數據

[英]Extraction of data from a file if pattern matches in 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時需要關閉文件讀取

一種方法是使用正則表達式,在以下模式中,我使用正向查找來匹配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'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM