Python Regex matches string as pattern and return number

I have some lines that represent some data in a text file. They all have the following format:

s = 'TheBears      SUCCESS Number of wins : 14'

They all start with a name, then a space and the text “SUCCESS Number of victories:” and, finally, the number of victories, n1. There are several lines, each of which has a different name and meaning. I am trying to write a program that can parse any of these strings and return the dataset name and numeric value at the end of the string. I am trying to use regular expressions for this, and I came up with the following:

import re
def winnumbers(s):
    pattern = re.compile(r"""(?P<name>.*?)     #starting name
                             \s*SUCCESS        #whitespace and success
                             \s*Number\s*of\s*wins  #whitespace and strings
                             \s*\:\s*(?P<n1>.*?)""",re.VERBOSE)
    match = pattern.match(s)

    name = match.group("name")
    n1 = match.group("n1")

    return (name, n1)

, . " :", , . , . ? , . - , .

, float(), n1 , , .

+5
3

:

((\S+)\s+SUCCESS Number of wins : (\d+))

:

>>> regex = re.compile("((\S+)\s+SUCCESS Number of wins : (\d+))")
>>> r = regex.search(string)
>>> r
<_sre.SRE_Match object at 0xc827cf478a56b350>
>>> regex.match(string)
<_sre.SRE_Match object at 0xc827cf478a56b228>

# List the groups found
>>> r.groups()
(u'TheBears SUCCESS Number of wins : 14', u'TheBears', u'14')

# List the named dictionary objects found
>>> r.groupdict()
{}

# Run findall
>>> regex.findall(string)
[(u'TheBears SUCCESS Number of wins : 14', u'TheBears', u'14')]
# So you can do this for the name and number:
>>> fullstring, name, number = r.groups()

, .

+2

, . , , ( , , ):

dict((line[:line.lower().index('success')+1], line[line.lower().index('wins:') + 6:]) for line in text.split('\n') if 'success' in line.lower())

, , :

output={}
for line in text:
    if 'success' in line.lower():
        words = line.strip().split(' ')
        output[words[0]] = words[-1]
+2

, . , . split(), , :

>>> def winnumber(s):
...     parts = s.split('SUCCESS Number of wins : ')
...     return (parts[0].strip(), int(parts[1]))
... 
>>> winnumber('TheBears      SUCCESS Number of wins : 14')
('TheBears', 14)

, (, , ), float() - - int(), .

Change . Obviously, this will only work for single lines - if you call the function in multiple lines, it will give you errors. To process the entire file, I would use map():

>>> map(winnumber, open(filename, 'r'))
[('TheBears', 14), ('OtherTeam', 6)]

Also, I'm not sure about your final use for this code, but it might be easier for you to work with outputs as a dictionary:

>>> dict(map(winnumber, open(filename, 'r')))
{'OtherTeam': 6, 'TheBears': 14}
+1
source

All Articles