Python 正则表达式不适用于if语句

Python 正则表达式不适用于if语句,python,regex,python-2.7,Python,Regex,Python 2.7,我正在使用下面的正则表达式从文件中提取一些信息 def inventory(): with open('inventory.log', 'r+') as f: match_hostname = re.compile(r'NAME: "(.+)",') match_pid = re.compile(r'PID: (.+) ,') match_sn = re.compile(r'SN: (.+)') list_text = [t

我正在使用下面的正则表达式从文件中提取一些信息

def inventory():
    with open('inventory.log', 'r+') as f:
        match_hostname = re.compile(r'NAME: "(.+)",')
        match_pid = re.compile(r'PID: (.+) ,')
        match_sn = re.compile(r'SN: (.+)')
        list_text = [text.strip() for text in f]
        for line in list_text:
            match_regex_hostname = match_hostname.search(line)
            match_regex_pid = match_pid.search(line)
            match_regex_sn = match_sn.search(line)
            if match_regex_hostname:
                final_hostname = match_regex_hostname.group(1).strip(" ")
                print final_hostname
            elif match_regex_pid:
                final_pid = match_regex_pid.group(1).strip(" ")
                print final_pid
            elif match_regex_sn:
                final_sn = match_regex_sn.group(1).strip(" ")
                print final_sn
inventory()
下面是“inventory.log”文件的内容


当我调用函数时,它不会显示final\u sn的结果。我试图对if语句重新排序,结果显示它只适用于if和第一个elif语句。我的代码有什么遗漏吗

如果在同一行上同时有
PID
SN
,则只运行第一条
elif
语句

试试这个:

def inventory():
with open('inventory.log', 'r+') as f:
    match_hostname = re.compile(r'NAME: "(.+)",')
    match_pid = re.compile(r'PID: (.+) ,')
    match_sn = re.compile(r'SN: (.+)')
    list_text = [text.strip() for text in f]
    for line in list_text:
        match_regex_hostname = match_hostname.search(line)
        match_regex_pid = match_pid.search(line)
        match_regex_sn = match_sn.search(line)
        if match_regex_hostname:
            final_hostname = match_regex_hostname.group(1).strip(" ")
            print final_hostname
        if match_regex_pid:
            final_pid = match_regex_pid.group(1).strip(" ")
            print final_pid
        if match_regex_sn:
            final_sn = match_regex_sn.group(1).strip(" ")
            print final_sn
inventory()

如果在同一行上同时有
PID
SN
,则只会运行第一条
elif
语句

试试这个:

def inventory():
with open('inventory.log', 'r+') as f:
    match_hostname = re.compile(r'NAME: "(.+)",')
    match_pid = re.compile(r'PID: (.+) ,')
    match_sn = re.compile(r'SN: (.+)')
    list_text = [text.strip() for text in f]
    for line in list_text:
        match_regex_hostname = match_hostname.search(line)
        match_regex_pid = match_pid.search(line)
        match_regex_sn = match_sn.search(line)
        if match_regex_hostname:
            final_hostname = match_regex_hostname.group(1).strip(" ")
            print final_hostname
        if match_regex_pid:
            final_pid = match_regex_pid.group(1).strip(" ")
            print final_pid
        if match_regex_sn:
            final_sn = match_regex_sn.group(1).strip(" ")
            print final_sn
inventory()
看起来“inventory.log”是一个csv文件,这意味着您可以使用该模块完成这项工作

def inventory(filename):
    with open(filename) as f:
        reader = csv.reader(f)
        for row in reader:
            print([item.strip()
                   for item in row if item.strip().lower().startswith(('name', 'pid', 'sn'))])
演示:

看起来“inventory.log”是一个csv文件,这意味着您可以使用该模块完成这项工作

def inventory(filename):
    with open(filename) as f:
        reader = csv.reader(f)
        for row in reader:
            print([item.strip()
                   for item in row if item.strip().lower().startswith(('name', 'pid', 'sn'))])
演示:


在给出的示例日志文件中,您的
PID
SN
位于同一行。因此,只调用第一个
elif
。如果您想从同一行同时获得
PID
SN
,最好只执行三个独立的
If
语句。您好,您是对的。我试图修改日志文件,将序列号放在新行,它就工作了。在给出的示例日志文件中,您的
PID
SN
在同一行。因此,只调用第一个
elif
。如果您想从同一行同时获得
PID
SN
,最好只执行三个独立的
If
语句。您好,您是对的。我试着修改日志文件,把序列号放在新行,它就工作了。