Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing text files using Python

Tags:

python

parsing

I am very new to Python and am looking to use it to parse a text file. The file has between 250-300 lines of the following format:

---- Mark Grey ([email protected]) changed status from Busy to Available @ 14/07/2010 16:32:36 ----
----  Silvia Pablo ([email protected]) became Available @ 14/07/2010 16:32:39 ----

I need to store the following information into another file (excel or text) for all the entries from this file

UserName/ID  Previous Status New Status Date Time

So my result file should look like this for the above entried

Mark Grey/[email protected]  Busy Available 14/07/2010 16:32:36
Silvia Pablo/[email protected]  NaN  Available 14/07/2010 16:32:39

Thanks in advance,

Any help would be really appreciated

like image 538
user392409 Avatar asked Nov 27 '22 05:11

user392409


1 Answers

To get you started:

result = []
regex = re.compile(
    r"""^-*\s+
    (?P<name>.*?)\s+
    \((?P<email>.*?)\)\s+
    (?:changed\s+status\s+from\s+(?P<previous>.*?)\s+to|became)\s+
    (?P<new>.*?)\s+@\s+
    (?P<date>\S+)\s+
    (?P<time>\S+)\s+
    -*$""", re.VERBOSE)
with open("inputfile") as f:
    for line in f:
        match = regex.match(line)
        if match:
            result.append([
                match.group("name"),
                match.group("email"),
                match.group("previous")
                # etc.
            ])
        else:
            # Match attempt failed

will get you an array of the parts of the match. I'd then suggest you use the csv module to store the results in a standard format.

like image 93
Tim Pietzcker Avatar answered Nov 29 '22 18:11

Tim Pietzcker