2

I'm trying to parse data from a text file that has lines like:

On 1-1-16 1:48 Bob used: 187
On 1-5-16 2:50 Bob used: 2

I want to print only the time and the number used, so it would look like:

1-1-16, 1:48, 187
1-5-16, 2:50, 2

I'm using this regex:

print(re.search(r"On ([0-9,-, ]+)Bob used ([0-9\.]+)", line.strip()))

I get results that say <_sre.SRE_Match object; span=(23, 26), match='Bob used: 187'>

I tried using .group() but it give the error "'NoneType' object has no attribute 'group'" I also noticed its only finding the second grouping (the number) and not the first (the date and time).

How can this be fixed?

3 Answers 3

1

You are missing the : after the Bob used and you need are more precise expression for the date part - for instance, \d+-\d+-\d+ \d+:\d+:

>>> s = 'On 1-1-16 1:48 Bob used: 187 On 1-5-16 2:50 Bob used: 2'
>>> re.search(r"On (\d+-\d+-\d+ \d+:\d+) Bob used: ([0-9\.]+)", s).groups()
('1-1-16 1:48', '187')
Sign up to request clarification or add additional context in comments.

2 Comments

Something tells me that's transcription typo, since the print gets hits, but depending on the code, the error code occur in the search used for .group(), but not the one used with print. Good catch regardless.
Thanks this solved it. ShadowRanger's answer also helped because it was failing after the first time it got a "None". I wish I could upvote both, thanks for your help guys.
1

You didn't give enough information on how you're using it, but since you're getting a Match object back, it shouldn't be None when you call .group() unless you're failing to store the result to the correct place. Most likely you are processing many lines, some of which match, and some of which don't, and you're not checking whether you matched before accessing groups.

Your code should always verify it got a Match before working with it further; make sure your test is structured like:

match = re.search(r"On ([0-9,-, ]+)Bob used ([0-9\.]+)", line.strip())
if match is not None:
    ... do stuff with match.group() here ...
... but not here ...

Comments

1

I'm pretty new to regular expressions myself however I came up with this

import re
source = "On 1-1-16 1:48 Bob used: 187\nOn 1-5-16 2:50 Bob used: 2" 
x=re.finditer('([0-9]-)+[0-9]+',source)
y=re.finditer('[0-9]+:[0-9]+',source)
z=re.finditer(': [0-9]*',source)
L = []
for i,j,k in zip(x,y,z):
    L.append((i.group(), j.group(), k.group().replace(': ', '') ))

print(L)

output

[('1-1-16', '1:48', '187'), ('1-5-16', '2:50', '2')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.