我正在尝试从文本文件中直接进行字符串匹配。有时,匹配意味着一个字符串包含多个目标字符串。目前,我的代码看起来就像
interesting_matches = [
"sys/bootdisk.py",
" engine stalled for ",
" changed to stalled)",
"DSR failure",
"Detected IDI failure",
"idi_shallow_verify_failure",
"Malformed block history",
"Out of order sequence message on",
"Port reset timeout of",
"gmp_info",
"test_thread",
" panic @ time ",
": *** FAILED ASSERTION",
"filesystem full",]
for match in interesting_matches:
# Iterate through simple matches.
if match in line:
processed_line_data = self._process_line(
match,
line,
line_datetime,
line_num,
current_version)
if "kern_sig" in line and "pid" in line:
processed_line_data = self._process_line(
("kern_sig", "pid"),
line,
line_datetime,
line_num,
current_version)
if "vfs_export" in line and "ignoring" in line:
processed_line_data = self._process_line(
("vfs_export", "ignoring"),
line,
line_datetime,
line_num,
current_version)
if "job_d" in line and
"State transition from state " in line and
" took longer than " in line:
processed_line_data = self._process_line(
(
"job_d",
"state transition from state",
" took longer than "),
line,
line_datetime,
line_num,
current_version)
if processed_line_data is not None:
return_list.append(processed_line_data)我想做的是类似于
interesting_matches = [
"sys/bootdisk.py",
" engine stalled for ",
" changed to stalled)",
"DSR failure",
"Detected IDI failure",
"idi_shallow_verify_failure",
"Malformed block history",
"Out of order sequence message on",
"Port reset timeout of",
"gmp_info",
"test_thread",
" panic @ time ",
": *** FAILED ASSERTION",
"filesystem full",
("kern_sig", "pid"),
("vfs_export", "ignoring"),
("job_d", "State transition from state", " took longer than "),]
for matches in interesting_matches
if any(match in line for match in matches):
processed_line_data = self._process_line(
match,
line,
line_datetime,
line_num,
current_version)但是元组和字符串的混合会导致值错误,说明不能比较字符串和元组。
如果要检查单个字符串和多个字符串,如何编写单个比较?
编辑:
以下是基于肖恩答案的工作代码
interesting_matches = [
("sys/bootdisk.py",),
(" engine stalled for ",),
(" changed to stalled)",),
("DSR failure",),
("Detected IDI failure",),
("idi_shallow_verify_failure",),
("Malformed block history",),
("Out of order sequence message on",),
("Port reset timeout of",),
("gmp_info",),
("test_thread",),
(" panic @ time ",),
(": *** FAILED ASSERTION",),
("filesystem full",),
("kern_sig", "pid"),
("vfs_export", "ignoring"),
("job_d", "State transition from state", " took longer than "),]
for matches in interesting_matches:
if all(match in "test_thread" for match in matches):
print(matches)发布于 2015-02-12 07:44:51
非常可行。尝试如下:对于单个子字符串签名,无论如何,将它们包装在一个元组中。现在,您的列表是同构的,您的问题变得更简单了。对于每个签名元组,请检查签名中的所有子字符串是否也在行中。
https://stackoverflow.com/questions/28471637
复制相似问题