2

我需要解析以下输出:-

|------------------------------------------|------| --------------------------------------|--------- ------------|------------|
| 组件名称 | 用户名 | 路径 | 开始时间 | 状态 |
|----------127.0.0.1------------|------|------外壳版本 1.2.1-13-09-27-----|--------|----------- -|
|ng40core2 |ng40 |/home/regress/ng40core2 |2013-10-07 16:55:52 |运行 |
|ng40core1 |ng40 |/home/regress/ng40core1 |2013-10-07 16:53:54 |运行 |
|------------------------------------------|------| --------------------------------------|--------- ------------|------------|

此输出中可能有多个具有不同版本 ng40core 的条目。

我已经为单行编写了正则表达式,

regex_list = ['\s*',
'\S+\s*',
'\S+\s+Assembly\s+name\s+\S+\s+User\s+name\s+\S+\s+Path\s+\S+\s+Start\s+Time\s+\S+\s+State\s+\S+\s*',
'\|\S+\d+\.\d+\.\d+\.\d+\S+Shell\s+version\s+.*\s*',
'\|(?P<ng40core_instance>\S+)\s+\|(?P<user_name>\S+)\s+\|(?P<path>\S+)\s+\|(?P<start_time>\d+\-\d+\-\d+\s+\d+:\d+:\d+)\s+\|(?P<state>\w+)\s+\|\s*']

我想为单个键获取多个值。
对于“ng40core2”——我需要用户名、路径、开始时间和状态
“ng40core1”的方式相同——我需要用户名、路径、开始时间和状态。

如果您能提出实现这一目标的方法,那将非常有帮助。

4

2 回答 2

4

您不需要使用正则表达式进行解析。

你的文字:

s = """
|------------------------------|-----------------|----------------------------------------|--------------------|------------|
| Assembly name                | User name       | Path                                   | Start Time         | State      |
|----------127.0.0.1-----------|-----------------|------Shell version 1.2.1-13-09-27------|--------------------|------------|
|ng40core2                     |ng40             |/home/regress/ng40core2                 |2013-10-07 16:55:52 |Running     |
|ng40core1                     |ng40             |/home/regress/ng40core1                 |2013-10-07 16:53:54 |Running     |
|------------------------------|-----------------|----------------------------------------|--------------------|------------|
"""

编码:

for line in s.splitlines():
    line = [x for x in line.split('|') if x]
    if line and line[0].startswith('ng'):
        line = [x.strip() for x in line] # cleanup whitespace
        assembly_name, user_name, path, start_date, state = line
        print assembly_name, user_name, path, start_date, state

结果:

>>> 
ng40core2  ng40  /home/regress/ng40core2  2013-10-07 16:55:52  Running 
ng40core1  ng40  /home/regress/ng40core1  2013-10-07 16:53:54  Running 

只是为了好玩,我做了一个更强大的功能:

def retrieve(file_path):
    with open(file_path) as f:
        for assembly_name, user_name, path, start_date, state in parse(f.read()):
            # code
            print assembly_name, user_name, path, start_date, state # example

def parse(text):
    for line in text.splitlines():
        line = [x for x in line.split('|') if x]
        if line and line[0].startswith('ng'):
            yield [x.strip() for x in line]
于 2013-10-08T10:19:25.627 回答
2

您可以使用re.findall()正则表达式来获取所需的行

print re.findall(r'\|(?P<ng40core_instance>\S+)\s+\|(?P<user_name>\S+)\s+\|(?P<path>\S+)\s+\|(?P<start_time>\d+\-\d+\-\d+\s+\d+:\d+:\d+)\s+\|(?P<state>\w+)\s+\|\s*', text)

输出:

[('ng40core2', 'ng40', '/home/regress/ng40core2', '2013-10-07 16:55:52', '正在运行'), ('ng40core1', 'ng40', '/home/回归/ng40core1','2013-10-07 16:53:54','正在运行')]

于 2013-10-08T12:10:48.433 回答