pyparsing Catch-22

7stud bbxx789_05ss at yahoo.com
Mon Apr 16 04:27:32 EDT 2007


Word("ABC", "def") matches "C", "Added", "Beef"
but not "BB", "ACE", "ADD"

That is just baffling.  There's no explanation that the characters
specified in the first string are used to match the first char of a
word and that the characters specified in the second string are used
to match the rest of the word.   It would also help to know that if
only one string is specified, then the specified characters will be
used to match all the chars in a word.  I think you should add a
simple example to your wiki that explains all that.

Also, I think you should state right up front that alphas is a string
made up of the chars a-zA-z and that nums is a string made up of the
chars 0-9.   That way when someone sees Word(alphas), they will
understand exactly what that means.  Also since matching any char is a
pretty common thing, I think you should mention what printables is as
well.

In any case this is the example I applied pyparsing to:

Given .txt file(all on one line).  Requirement--construct a list from
the text:
-------------
mara = [
'U2FsdGVkX185IX5PnFbzUYSKg+wMyYg9',
'U2FsdGVkX1+BCxltXVTQ2+mo83Si9oAV0sasmIGHVyk=',
'U2FsdGVkX18iUS8hYBXgyWctqpWPypVz6Fj49KYsB8s='
]
-----------

and this is what I came up with:

----------
from pyparsing import Word, alphas, commaSeparatedList

name = Word(alphas)
lookFor = name + "=" + "[" + commaSeparatedList + "]"

my_file = open("aaa.txt")
for line in my_file:
    alist  = lookFor.parseString(line)

globals()[alist[0] ] = [ alist[3].strip("'"), alist[4].strip("'"),
alist[5].strip("'") ]

print mara[2]
----------

Any tips?




More information about the Python-list mailing list