Pattern matching from a text document

infidel saint.infidel at
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

pat =

ph = re.compile(pat,re.IGNORECASE)
players = []
for line in file('playerlist.txt'):
    match = ph.match(line)
    player = {
        'forename' :,
        'surname' :,
        'attacking' :,
        'defending' :,
        'fitness' : 

More information about the Python-list mailing list