在保存数据的文件中:
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时,文件读取需要关闭
发布于 2015-03-05 11:44:52
一种方法是使用regex,在下面的模式中,我使用正面环顾来匹配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']请注意,如果您想从文件中读取此字符串,则需要在open('file_name').read()函数中使用s而不是s。
发布于 2015-03-05 11:56:41
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'])https://stackoverflow.com/questions/28876856
复制相似问题