Pattern matching from a text document

Larry Bates lbates at syscononline.com
Thu Mar 24 08:28:14 EST 2005


Ben,

Others have answered your specific questions, but I thought
I'd use this opportunity to make a general statement.  Unlike
other programming languages, Python doesn't make its built-in
functions keywords.  You should never, ever, ever name a
variable 'list' (the same is true of dict, tuple, str, ...).
When you do you mask the built-in Python function with your
variables.  If this hasn't bitten you before, it will at some
point.

It really doesn't sound like you require regular expression
complexity to just read in some data.  You might want to
investigate CSV module (for reading comma delimited files)
or you might just be able to use simple .split() method (for
tab delimited files).

Hope info helps.

Regards,
Larry Bates


Ben wrote:
> I'm currently trying to develop a demonstrator in python for an
> ontology of a football team. At present all the fit players are
> exported to a text document.
> 
> The program reads the document in and splits each line into a string
> (since each fit player and their attributes is entered line by line in
> the text document) using list = target.splitlines()
> 
> The program then performs a loop like so:
> 
> while foo > 0:
>     if len(list) == 0:
>         break
>     else:
>         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)
> 
>         match = ph.match(list[1])
> 
>         forename = match.group(1)
>         surname = match.group(3)
>         attacking = match.group(7)
>         defending = match.group(8)
>         fitness = match.group(9)
> 
>         print forename
>         print len(list)
>         del list[0]
> 
> The two main problems I'm having are that the first and entry in the
> list is not printing. Once I have overcome this problem I then need
> each player and there related variables to be stored seperately. This
> is not happening at present because each time the loop runs it
> overwrites the value in each variable.
> 
> Any help would be greatly appreciated.
> 
> Ben.
> 



More information about the Python-list mailing list