how to write file with cp1250 encodings?

G

Grzegorz Smith

Hi all. I have got situation: i load data from database(MSSQL) wchich are
encoded cp1250 and I fill template with that data (Cheetah Template), after
all i want to save template to file on disk. I'm using
newfile = open("template.html",w")
newfile.write(str(template))
newfile.close()
But data encodings are saved wrong, and I try diffrent programs to convert
but all i get was only mess. So is this any way to save data with encodings
cp1250 properly?
Any help will be very appreciated
Gregor
 
W

Waldemar Osuch

Grzegorz said:
Hi all. I have got situation: i load data from database(MSSQL) wchich are
encoded cp1250 and I fill template with that data (Cheetah Template), after
all i want to save template to file on disk. I'm using

One way to do it:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=cp1250" http-equiv="content-type">
<title>Welcome</title>
</head>
<body>
Welcome to $city
<br>
</body>

The idea here is to encode your unicode before passing it to the
template.

Waldemar
 
M

Magnus Lycka

Grzegorz said:
Hi all. I have got situation: i load data from database(MSSQL) wchich are
encoded cp1250

Are you sure that you are getting cp1250 from the database?
How do you communicate with the database? With the database
APIs I used in Windows, I always got Unicode objects from
databases, how ever things were internally stored in the DB.

If you have just managed to get your data correctly into
Unicode objects, you can convert them into whatever code
page you like with the .encode(encoding) method. Just make
sure you encode with the same encoding that yo state in
the meta element charset attribute.
 
J

jean-michel bain-cornu

Hi,
Grzegorz said:
Hi all. I have got situation: i load data from database(MSSQL) wchich are
encoded cp1250 and I fill template with that data (Cheetah Template), after
all i want to save template to file on disk. I'm using
newfile = open("template.html",w")
newfile.write(str(template))
newfile.close()
But data encodings are saved wrong, and I try diffrent programs to convert
but all i get was only mess. So is this any way to save data with encodings
cp1250 properly?
Any help will be very appreciated
Gregor
I had to do it recently, reading from mysql and pushing data into a
browser by cherryPy.
I used :
try:
val= dbString.decode('utf8').encode('iso-8859-1')
except UnicodeDecodeError:
val= dbString
The 'try:' was needed because of some extra-character on top of the 7
bits limit raising an exception.
I have no more explanations about that, and I must say it was a pain in
the neck to find a solution.
I guess in your case, you have to replace iso-8859-1 by something else
in the case it's not strictly the same that cp1250. The '4.9.2 Standard
Encodings' documentation section give some explanations.
Regards,
jm
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top