[Tutor] about python program
Michael Janssen
Janssen@rz.uni-frankfurt.de
Sun Mar 23 05:08:02 2003
On Sun, 23 Mar 2003, Abdirizak abdi wrote:
>
> Hi everyone,
>
> I really need some help, I am working on program that tokinzes a
you can help yourself, by print all tokens before processen. Python print
statement is clever enough to print many kind of data including tuples and
you will see which results from your regexp are delivered as tuple ("named
group" as an indication :-) compare the Libary for the syntax and meaning
of grouping parens "()" ).
Michael
PS: please make shure, that the traceback is from the same code you send
to us. Your code would have triggered that error already in line:
if token.startswith(" "): (unless something very strange is going on).
Otherwise we lost time while checking if something very strange is going
on ;-)
> string and the program captures various patterns and I want to process
> each pattern separatelyby using some other functions to extract some
> information and format for display, I amusing conditional statement to
> evaluate the begining of each pattern and then call a function to
> process and display. I am trying to use a built-in-function
> startwith()but it is giving me an error message which is like this:
>
> File "Ass-002.py", line 65, in markup
> if token.startswith("<EQN/>"):
> ttributeError: 'tuple' object has no attribute 'startswith'
>
> here is the program:
>
> def markup(line, tag='W'):
> """ this function tags <w> and </w> to a text """
>
> exp_token = re.compile(r"""
> ([-a-zA-Z0-9_]+| # for charcterset
> [\"\'.\(),:!\?]| # symbol chracters
> (?:<EQN/>)(?P<text>.*?)(?:<EQN/>)| #matches <
> <REF SELF='YES'>.*?</REF>)
> """, re.VERBOSE )
> spacing = " "
> result = []
>
> #call all the matching regular expression
>
> token_list = exp_token.findall(line)
> for token in token_list:
> evaluate each token as you go and call the appropriate function
> if token.startswith(" "):
> empty_str = empty_string(token)
> result.append(empty_str)
>
> if token.startswith("<EQN/>"):
> EQN_result = EQNfunc(token)
> result.append(EQN_result)
> print
> result.append(text1)
> #for testing purposes
> else:
> print "error:", token
>
> #print '<%s>%s</%s>%s' %(tag,token,tag,spacing)
> #result.append('<%s>%s</%s>%s' %(tag,token,tag,spacing) )
>
>
>
>
> thanks in advance
> return result
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!