is there a quick and easy way to conver the funny characters to char
entities so i can store them?
or is there existing libraries?
cheers
> ive got a string "Il Est Né" and i want to put it into a xml document
>
> is there a quick and easy way to conver the funny characters to char
> entities so i can store them?
I recommend you properly declare the encoding of the document, e.g.
<?xml version="1.0" encoding="iso-8859-1"?>
(assuming the é is meant to be encoded in Latin-1).
XML does not define external entities for non-ASCII characters, so you
would have to create a DTD first if you want them.
Regards,
Martin
on text that has the funny chars in it. it crashes with an error like this
Traceback (most recent call last):
File "./batch-maid.py", line 85, in ?
generate_browse_options()
File "./batch-maid.py", line 70, in generate_browse_options
f.write(options_composer)
UnicodeError: ASCII encoding error: ordinal not in range(128)
whats going on?
how can i fix it?
cheeers
"Martin v. Loewis" <mar...@v.loewis.de> wrote in message
news:m3elbuz...@mira.informatik.hu-berlin.de...
> Traceback (most recent call last):
> File "./batch-maid.py", line 85, in ?
> generate_browse_options()
> File "./batch-maid.py", line 70, in generate_browse_options
> f.write(options_composer)
> UnicodeError: ASCII encoding error: ordinal not in range(128)
>
> whats going on?
You should start with telling us what f is, and what options_composer
is. How are we supposed to know?
> how can i fix it?
I assume f is a file object, and options_composer is a Unicode
object. In that case, you need to determine the encoding you want to
use, and encode the Unicode object, e.g. with
f.write(options_composer.encode("utf-8"))
Regards,
Martin
Characters like "é" aren't generally converted to entities in XML,
unlike in various versions of HTML which had things like "é"
and which seemed overly ASCII-friendly. What I would recommend is that
you obtain a Unicode string from the string that you've been given, if
it isn't already Unicode (but I would guess by your enquiry that it
isn't), and then use the various XML APIs to write it into the
document.
Of course, if the string isn't Unicode, you have to know what encoding
was used in the creation of the string. For example:
>>> s = "Il est né!"
>>> s
'Il est n\x82!'
>>> u = unicode(s)
At this point, some people naively assume that a Unicode string will
be created, but since most character sets do not assign "é" to "\x82",
Python can't be sure that you want to create "é" in the resulting
Unicode string.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeError: ASCII decoding error: ordinal not in range(128)
>>> u = unicode(s, "iso-8859-1")
Here, we've supplied a character encoding. Now, Python knows what to
do with that "\x82" character.
>>> print u
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> print u.encode("iso-8859-1")
Il est né!
To print things out, you need to convert the Unicode string to the
appropriate native encoding.
Of course, there are various shortcuts that can be taken, especially
if you're not using the XML APIs, but are instead writing strings
directly to the XML file. In such cases, you can use an encoding
declaration at the start of the document...
<?xml version="1.0" encoding="iso-8859-1"?>
...and then not bother converting the strings to Unicode, but an
awareness of Unicode is probably invaluable if you're going to be
doing a fair amount of XML work.
Paul
I've got a content syndication application that sends out XML, and
some of the recipients have a surprisingly hard time coping with
UTF-8. So I supply them with ASCII, after escaping everything over 7
bits to a character reference. This also guarantees proper display in
Internet Explorer.
>>> s = 'Il est n\x82!'
>>> u = unicode(s, 'iso-8859-1')
>>> L = []
>>> for char in u:
val = ord(char)
if val > 127:
L.append('&#%s;' % val)
else:
L.append(char)
>>> u2 = ''.join(L)
>>> u2
u'Il est n‚!'
>>> s2 = u2.encode('ascii')
>>> s2
'Il est n‚!'
-- Wade Leftwich
Ithaca, NY