472,353 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

xml processing and sys.setdefaultencoding

hi,
i wrote a small application which extracts a javadoc similar documentation
for xslt stylesheets using python, xslt and pyana.
using non-ascii characters was a problem. so i set the defaultending to
UTF-8 and now everything works (at least it seems so, need to do more
testing though).

it may not be the most elegant solution (according to python in a nutshell)
but it almost seems when doing xml processing it is mandatory to set the
default encoding. xml processing should almost only work with unicode
strings and this seems the easiest solution.

any comments on this? better ways to work

thanks
chris
Jul 18 '05 #1
1 2475
christof hoeke wrote:
i wrote a small application which extracts a javadoc similar
documentation
for xslt stylesheets using python, xslt and pyana.
using non-ascii characters was a problem.
That's odd. Did your stylesheets contain non-ascii characters? If yes,
did you declare the character encoding at the beginning of the
document, e.g.

"<?xml version="1.0" encoding="iso-8859-1"?>
so i set the [python] defaultending to
UTF-8 and now everything works (at least it seems so, need to do more
testing though).
If you don't put an encoding declaration in your XML documents
(including XSLT style/transform sheets), then an XML parser would by
default treat the document content as UTF-(8|16), as the XML standard
mandates.

Are you working from XML documents which are stored as strings inside
a python module? In which case, your special characters will actually
be encoded in whatever encoding your python module is stored. So you
might need to put an encoding declaration on your python module:-

http://www.python.org/peps/pep-0263.html
it may not be the most elegant solution (according to python in a
nutshell)
but it almost seems when doing xml processing it is mandatory to set the
default encoding. xml processing should almost only work with unicode
strings and this seems the easiest solution.
It is always recommended to explicitly state the encoding on your XML
documents. If you don't, then the parser assumes UTF-(8|16). If your
documents aren't really UTF-(8|16), then you will get seemingly random
mapping of characters to other characters.
any comments on this? better ways to work


If you're not dealing specifically with ASCII, then declare your
encodings, in both your python modules and your xml documents. Find
out what is the default character set used by your text editor. Find
out how to change which character set is in use.

If you create, sell or maintain text editing or processing software,
make it easy for your users to find out what character encodings are
in effect.

HTH,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Askari | last post by:
Where is the method : "sys.setdefaultencoding(name)"? Problem : I want change the default encoding because I want put french letter : ...etc...
0
by: Ganapathy | last post by:
I have COM dll code written in VC 6.0. When i tried compiling this code in VC 7, The MIDL cmpiler gets called twice. i.e. it initially compiles...
2
by: Luiz Vianna | last post by:
Hi folks, I got a problem that certainly someone had too. After a user request, I (my server) must process a lot of data that will expend some...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and...
10
by: Enrique Cruiz | last post by:
Hello all, I am currently implementing a fairly simple algorithm. It scans a grayscale image, and computes a pixel's new value as a function of...
0
ADezii
by: ADezii | last post by:
In the Tip of the Week #19, we demonstrated Transaction Processing, specifically as it applies to DAO (Data Access Objects). In this week's Tip,...
1
by: Robin Becker | last post by:
Can someone explain the rationale of making the default encoding a sitewide setting? I could live with the the default being set on a per process...
2
by: smalltalk | last post by:
>>import sys Traceback (most recent call last): File "<interactive input>", line 1, in <module> AttributeError: 'module' object has no attribute...
0
by: tavares | last post by:
(Our apologies for cross-posting. We appreciate if you kindly distribute this information by your co- workers and colleagues.) ...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.