Pattern matching from a text document

infidel saint.infidel at gmail.com
Wed Mar 23 20:42:15 CET 2005


First, if you're going to loop over each line, do it like this:

for line in file('playerlist.txt'):
    #do stuff here

Second, this statement is referencing the *second* item in the list,
not the first:

match = ph.match(list[1])

Third, a simple splitting of the lines by some delimiter character
would be easier than regular expressions, but whatever floats your
boat.  If you insist on using regexen, then you should compile the
pattern before the loop.  No need to do it over and over again.

Fourth, if you want to create a list of players in memory, then you
need either a class or some other structure to represent each player,
and then you need to add them to some kind of list as you go.  Like
this:

pat =
"([a-z]+)(\s+)([a-z]+)(\s+)([a­-z]+)(\s+)(\d{1})(\d{1})(\d{1}­)(\d{1})(\d{1})([a-z]+)"

ph = re.compile(pat,re.IGNORECASE)
players = []
for line in file('playerlist.txt'):
    match = ph.match(line)
    player = {
        'forename' : match.group(1),
        'surname' : match.group(3),
        'attacking' : match.group(7),
        'defending' : match.group(8),
        'fitness' : match.group(9) 
    }
    players.append(player)




More information about the Python-list mailing list