【发布时间】:2020-10-08 13:47:09
【问题描述】:
我正在尝试编写一个 Python 脚本来解析一个日志文件。脚本核心借自pythonic ways。
import re
log_file_path = r"O:\ZTK log file parser\2 Parsing Log\JP"
regex = '8355371640847825590'
match_list = []
with open(log_file_path, "r") as file:
for line in file:
for match in re.finditer(regex, line, re.S):
match_text = match.group()
match_list.append(match_text)
print(match_list) # work in progress
上面的例子在解析纯字符串值时效果很好。但是当我尝试插入正则表达式变量时:
regex = '((.*\n){2}).*8355371640847825590'
它总是返回一个空列表。
困扰我的是,这个表达式在测试环境中运行得非常好,比如https://regex101.com/。每个值都正确匹配。不幸的是,我无法在 Python 中复制它。
如果您能帮助我,我将不胜感激。
【问题讨论】:
-
当然,你逐行阅读。使用
file.read()阅读整个文件 -
@WiktorStribiżew 这很重要!但是,我的代码现在吐出了整个文件: log_file_path = r"O:\ZTK log file parser\2 Parsing Log\JP" regex = '((.*\n){2}).*8355371640847825590' match_list = [ ] f = open(log_file_path, "r") with open(log_file_path, "r") as file: for line in file: for match in re.finditer(regex, f.read(), re.S): match_text = match.group() match_list.append(match_text)。它仍然不匹配正确的正则表达式:(
标签: python regex parsing logging