raw Strings from XML attributes

David Bolen db3l at fitlinxx.com
Fri Jun 11 12:34:31 EDT 2004


Karen Loughran <k.loughran at qub.ac.uk> writes:

> Hiya,
> 
> I'm reading a string attribute from an XML document.  The string
> contains newline characters and is treated as raw.
> I'm trying to match it with a string read from a file which is
> identical .  But because it isn't raw it won't match.
> 
> I've seen documentation for converting string 'literals' to raw form
> with r'Sample String'.  But is there a function which will convert my
> raw string read from XML to non-raw/escaped sequence format ?   Or to
> convert a string 'variable' read from a file to raw form ? ie, I'm not
> converting string 'literals' ?

If I understand you correctly I think you want repr() (or %r in a
format string).

This will produce a string representation of a Python object, which is
designed to make a best attempt at a string that could create the
original object when passed into eval().  While that doesn't
necessarily hold true for arbitrary objects, for strings it means that
it produces the raw/escaped format - thus distinguishing itself from
the str() function (or %s formatting operator) which attempts to
produce a nicely printable form.

For example:

    >>> test = 'Test\t\\ing'
    >>> print str(test)
    Test        \ing
    >>> print repr(test)
    'Test\t\\ing'

(Note that I replaced the literal tab in the first output with 8 spaces
 to ensure it works in the newsgroup posting, but the actual output from
 the interpreter was a literal tab)

-- David



More information about the Python-list mailing list