Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > encoding ascii data for xml

Reply
Thread Tools

encoding ascii data for xml

 
 
harrelson
Guest
Posts: n/a
 
      10-03-2008
I have a large amount of data in a postgresql database with the
encoding of SQL_ASCII. Most recent data is UTF-8 but data from
several years ago could be of some unknown other data type. Being
honest with myself, I am not even sure that the most recent data is
always UTF-8-- data is entered on web forms and I wouldn't be
surprised if data of other encodings is slipping in.

Up to the point I have just ignored the problem-- on the web side of
things everything works good enough. But now I am required to stuff
this data into xml datasets and I am, of course, having problems. My
preference would be to force the data into UTF-8 even if it is
ultimately an incorrect encoding translation but this isn't working.
The below code represents my most recent problem:

import xml.dom.minidom
print chr(3).encode('utf-8')
dom = xml.dom.minidom.parseString( "<test>%s</test>" %
chr(3).encode('utf-8') )

chr(3) is the ascii character for "end of line". I would think that
trying to encode this to utf-8 would fail but it doesn't-- I don't get
a failure till we get into xml land and the parser complains. My
question is why doesn't encode() blow up? It seems to me that
encode() shouldn't output anything that parseString() can't handle.

Sorry in advanced if this post is ugly-- it is through the google
groups interface and google mangles the entry sometimes.
 
Reply With Quote
 
 
 
 
Dillon Collins
Guest
Posts: n/a
 
      10-04-2008
On Friday 03 October 2008, harrelson wrote:
> import xml.dom.minidom
> print chr(3).encode('utf-8')
> dom = xml.dom.minidom.parseString( "<test>%s</test>" %
> chr(3).encode('utf-8') )
>
> chr(3) is the ascii character for "end of line". [...] My
> question is why doesn't encode() blow up?


You just answered your question. 0x03 may not be a printing character, but it
is a valid character in the ascii character set and therefore is not a
problem. For xml, however, it is an illegal character so that's why the
parser is throwing an error.
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-04-2008
On Fri, 03 Oct 2008 14:41:13 -0700, harrelson wrote:

> import xml.dom.minidom
> print chr(3).encode('utf-8')
> dom = xml.dom.minidom.parseString( "<test>%s</test>" %
> chr(3).encode('utf-8') )
>
> chr(3) is the ascii character for "end of line". I would think that
> trying to encode this to utf-8 would fail but it doesn't-- I don't get a
> failure till we get into xml land and the parser complains. My question
> is why doesn't encode() blow up? It seems to me that encode() shouldn't
> output anything that parseString() can't handle.


It's not a problem with encode IMHO but with XML because XML can't handle
all ASCII characters. XML parsers choke on every code below 32 that is
not whitespace. BTW `chr(3)` isn't "end of line" but "end of text" (ETX).

If you want to be sure that an arbitrary string can be embedded into XML
you'll have to encode it as base64 or something similar.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      10-04-2008
On Oct 4, 7:41*am, harrelson <harrel...@gmail.com> wrote:
> I have a large amount of data in a postgresql database with the
> encoding of SQL_ASCII. *Most recent data is UTF-8 but data from
> several years ago could be of some unknown other data type. *Being
> honest with myself, I am not even sure that the most recent data is
> always UTF-8-- data is entered on web forms and I wouldn't be
> surprised if data of other encodings is slipping in.
>
> Up to the point I have just ignored the problem-- on the web side of
> things everything works good enough. *But now I am required to stuff
> this data into xml datasets and I am, of course, having problems. *My
> preference would be to force the data into UTF-8 even if it is
> ultimately an incorrect encoding translation but this isn't working.
> The below code represents my most recent problem:
>
> import xml.dom.minidom
> print chr(3).encode('utf-8')
> dom = xml.dom.minidom.parseString( "<test>%s</test>" %
> chr(3).encode('utf-8') )
>
> chr(3) is the ascii character for "end of line". *I would think that
> trying to encode this to utf-8 would fail but it doesn't-- I don't get
> a failure till we get into xml land and the parser complains. *My
> question is why doesn't encode() blow up? *It seems to me that
> encode() shouldn't output anything that parseString() can't handle.


The encode method is doing its job, which is to encode ANY and EVERY
unicode character as utf-8, so that it can be transported reliably
over an 8-bit-wide channel. encode is *not* supposed to guess what you
are going to do with the output.

Perhaps instead of "forcing the data into utf-8", you should be
thinking about what is actually in your data. What is the context that
chr(3) appears in? Perhaps when you get around to print
repr(some_data), you might see things like "\x03harlie \x03haplin" --
it's a common enough keyboarding error to hit the Ctrl key instead of
the Shift key and unfortunately a common-enough design error for there
to be no checking at all.

BTW, there's no forcing involved -- chr(3) is *already* utf-8.

HTH,
John
 
Reply With Quote
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      10-04-2008
On Sat, 04 Oct 2008 12:18:13 -0700
Dennis Lee Bieber <> wrote:
> On 4 Oct 2008 06:59:20 GMT, Marc 'BlackJack' Rintsch <>
> declaimed the following in comp.lang.python:
>
> > not whitespace. BTW `chr(3)` isn't "end of line" but "end of text" (ETX).
> >

> Hmm, think I'll need to look up an ASCII chart -- I seem to recall
> ETX as "end of transmission"


Nope, Marc is correct. EOT, chr(4), is "end of transmission."

--
D'Arcy J.M. Cain <> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
[FR/EN] how to convert the characters ASCII(0-255) to ASCII(0-127) Alextophi Perl Misc 8 12-30-2005 10:43 AM
Default Charset Encoding in JSP page is ASCII Fritz Bayer Java 1 05-30-2005 06:01 PM
URI encoding ASCII, LATIN1 or UNICODE? Fritz Bayer Java 2 04-20-2005 01:19 PM
routine/module to translate microsoft extended ascii to plain ascii James O'Brien Perl Misc 3 03-05-2004 04:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57