Catherine Slaughter
Catherine Slaughter

Reputation: 25

Pulling out specific values from a string in Python

So I'm reading from a file some data on how long a program took to run. The text file looks like repeated "blocks" like this:

real 1m49.296s user 1m40.597s sys 0m7.979s

I need to pull out the minute and second values in the "user" and "sys" lines in order to sum them up. As of now, I have a for loop for the number of these "blocks" in the file and I'm reading through line by line, saving the real, user, and sys lines to different string variables. I don't think this is a great way of doing so, however, because it requires I hard-code the number of "blocks." Obviously I'm pretty new to this, any help is appreciated!

Upvotes: 0

Views: 166

Answers (1)

Boying
Boying

Reputation: 1434

Regular expression is your friend.

https://docs.python.org/3/library/re.html

Check the match group section:

(...)

Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use ( or ), or enclose them inside a character class: [(], [)].

Since you already know how to read the text file. Below is an example showing how to parse the text and do the calculation

import re
foo = """real    1m49.296s
user    1m40.597s
sys     0m7.979s
"""
total_sec = 0
for item in foo.splitlines():
    m = re.search(r"(real|user)\s+(\S+)m(\S+)s", item)
    if m:
        print(m.group(1), m.group(2), m.group(3))
        total_sec += (float(m.group(2))*60+float(m.group(3)))
        print("total %f" % total_sec)

Result:

('real', '1', '49.296')
total 109.296000
('user', '1', '40.597')
total 209.893000

Upvotes: 1

Related Questions