raw Strings from XML attributes

K

Karen Loughran

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' ?

Thanks
Karen
 
D

David Bolen

Karen Loughran said:
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\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
 
P

Peter Otten

Karen said:
. But because it isn't raw it won't match.

I fear you have a misconception about raw strings.

r"\abc"
"\\abc"

and

chr(92) + "abc"

are exactly the same strings when written in Python source code. If your
strings do not match, it has nothing to do with "rawness".

Peter
 
P

Paul Prescod

Karen said:
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.

As Peter Otten points out, "rawness" is a concept that Python forgets as
soon as a string is loaded into memory.

r"abc" == "abc"

r"\123abc" == "\\123abc"

So your problem is something else. Perhaps you could use the "repr"
function to print out the representations of the two strings and that
will give you more information.


print repr(string1)
print repr(string2)
print string1 == string2

Paul Prescod
 
P

Paul Prescod

Karen said:
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.

As Peter Otten points out, "rawness" is a concept that Python forgets as
soon as a string is loaded into memory.

r"abc" == "abc"

r"\123abc" == "\\123abc"

So your problem is something else. Perhaps you could use the "repr"
function to print out the representations of the two strings and that
will give you more information.


print repr(string1)
print repr(string2)
print string1 == string2

Paul Prescod
 
T

Terry Reedy

As Peter Otten points out, "rawness" is a concept that Python forgets as
soon as a string is loaded into memory.

More specifically, "raw" is an alternate mode of interpreting/translating
string literals in program code as they are used to give value to a string
object. Similarly, "text" is an alternative mode (for DOS/Win, not *nix,
don't know about Mac) for converting file bytes to a Python string. In
other words, 'rawness' is a concept of action rather than a concept of
thingness.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top