Parse Output for Python

2019-05-11 21:39发布

问题:

My software outputs these two types of output:

-rwx------ Administrators/Domain Users  456220672   0% 2018-04-16 16:04:40 E:\\_WiE10-18.0.100-77.iso

-rwxrwx--- Administrators/unknown        6677   0% 2018-04-17 01:33:23 E:\\program files\\cluster groups\\sql server (mssqlserver)\\logs\\progress-MOD-1523883344023-3001-Windows.log

I would like to get the file names from both outputs:

  • E:\\_WiE10-18.0.100-77.iso, for the first one
  • E:\\program files\\cluster groups\\sql server (mssqlserver)\\logs\\progress-MOD-1523883344023-3001-Windows.log, for the second one

If i use something like the code below, it won't work if the second parameter has spaces in it. It works if there aren't any spaces in the Domain Username.

for item in outputs:
    outputs.extend(item.split())
for item2 in [' '.join(outputs[6:])]:
    new_list.append(item2)

How can I get all the parameters individually, including the filenames?

回答1:

If regex is an option:

text = """-rwx------ Administrators/Domain Users  456220672   0% 2018-04-16 16:04:40 E:\\_WiE10-18.0.100-77.iso

-rwxrwx--- Administrators/unknown        6677   0% 2018-04-17 01:33:23 E:\\program files\\cluster groups\\sql server (mssqlserver)\\logs\\progress-MOD-1523883344023-3001-Windows.log"""

import re

for h in re.findall(r"^.*?\d\d:\d\d:\d\d (.*)",text,flags=re.MULTILINE):
    print(h)

Output:

E:\_WiE10-18.0.100-77.iso
E:\program files\cluster groups\sql server (mssqlserver)\logs\progress-MOD-1523883344023-3001-Windows.log

Pattern explained:

The pattern r"^.*?\d\d:\d\d:\d\d (.*)" looks for linestart '^' + as less anythings as possible '.*?' + the time-stamp '\d\d:\d\d:\d\d ' followed by a space and captures all behind it till end of line into a group.

It uses the re.MULTILINE flag for that.


Edit:

Capturing the individual things needs some more capturing groups:

import re

for h in re.findall(r"^([rwexXst-]+) ([^0-9]+) +\d+.+? +(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (.*)",text,flags=re.MULTILINE):
#                       ^^^^^^^^^^^^ ^^^^^^^^           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
#                          flags     grpName                               datetime          filename
    for k in h:
        print(k)
    print("")

Output:

-rwx------
Administrators/Domain Users 
2018-04-16 16:04:40
E:\_WiE10-18.0.100-77.iso

-rwxrwx---
Administrators/unknown       
2018-04-17 01:33:23
E:\program files\cluster groups\sql server (mssqlserver)\logs\progress-MOD-1523883344023-3001-Windows.log


回答2:

You could use a regular expression like

\b[A-Z]:\\\\.+


回答3:

Aside from using regex, you can try something similar to this.

output = '-rwx------ ... 2018-04-16 16:04:40 E:\\\\_WiE10-18.0.100-77.iso'

drive_letter_start = output.find(':\\\\')
filename = output[drive_letter_start - 1:]

It looks for the first occurrence of ':\\'and gets the drive letter before the substring (i.e. ':\\') and the full file path after the substring.

EDIT
Patrick Artner's answer is better and completely answers OP's question compared to this answer. This only encompasses capturing the file path. I am leaving this answer here should anyone find it useful.