我正在尝试逐行查看日志文件并找到字符串的实例,例如5x76fd63-df62-4dae-a92b-10b8f38fb275有 4 个破折号。我需要所有行,但想使用匹配的字符串作为键/ID。
1 回答
慕姐8265434
TA贡献1813条经验 获得超2个赞
我需要所有行,但想使用匹配的字符串作为键/ID。
由于您需要密钥,因此字典是合适的。
import re
keyline = {} # start with empty dictionary
for line in file:
m = re.search("\w+-\w+-\w+-\w+-\w+", line) # search ID with four dashes
if m:
keyline[m.group()] = line # store the line with its ID
print(keyline)
添加回答
举报
0/150
提交
取消
