ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] parsing--is this right?
by Danny Yoo other posts by this author
Jun 11 2002 9:17PM messages near this date
Re: [Tutor] parsing--is this right? [function evaluation / errors for fun and profit] | Re: [Tutor] parsing--is this right?
On Tue, 11 Jun 2002, Danny Yoo wrote:

>  And this is the source of the shallowness: we're just calling str().
>  But instead of directly calling str() on each piece in between, the
>  trick is to apply toXML() again to each inner piece!  That way, we
>  guarantee that the inner lists are also transformed properly:
> 

[code of toXML()]
>  def toXML(structure):
>     if type(structure) == type([]):
>         tag = structure[0][1:]  ## secondary slice removes leading '/'
>         text_pieces = [str(s) for s in structure[1:]]
>         return "<%(tag)s>%(text)s</%(tag)s>" % >               { 'tag' : tag,
>                  'text' : ''.join(text_pieces) }
>     else:
>         return str(structure)

>  ###
>  >>> def toXMLDeeply(structure):
>  ...     if type(structure) == type([]):
>  ...         tag = structure[0][1:]  ## secondary slice removes leading '/'
>  ...         text_pieces = [toXML(s) for s in structure[1:]]
>  ...         return "<%(tag)s>%(text)s</%(tag)s>" % > ...                { 'tag' : tag,
>  ...                  'text' : ''.join(text_pieces) }
>  ...     else:
>  ...         return str(structure)
>  ...
>  >>> print toXMLDeeply(parsed_doc)
>  <footnote><i>anitalicizedword</i><i>maybeanotheritalicizedword</i>text</footnote>
>  ###
> 
> 
>  The transformation wasn't perfect, because my parsing step had wiped out
>  whitespace in my parsed_doc, so that needs to be fixed.  Still, it almost
>  works.  *grin*


But note that if you have three levels of nesting, you'll still get weird
output, because the third level won't be marked up properly.  Yikes!

Ummm... I'll pretend that I didn't goof up, and say that if you understand
what's happening, you'll know how to fix this bug.  *grin*



_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Danny Yoo
Paul Tremblay
Danny Yoo
Danny Yoo

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved