Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   how to write file with cp1250 encodings? (http://www.velocityreviews.com/forums/t354881-how-to-write-file-with-cp1250-encodings.html)

Grzegorz Smith 02-27-2006 12:24 AM

how to write file with cp1250 encodings?
 
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

Waldemar Osuch 02-27-2006 05:25 AM

Re: how to write file with cp1250 encodings?
 
Grzegorz Smith wrote:
> 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:

>>> from Cheetah.Template import Template
>>> print open('city.tmpl').read()

<!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>
</html>

>>> t = Template(file='city.tmpl')
>>> city = u'Lódz'
>>> t.city = city.encode('cp1250')
>>> open('city.html', 'w').write(str(t))


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

Waldemar


Magnus Lycka 03-01-2006 01:07 PM

Re: how to write file with cp1250 encodings?
 
Grzegorz Smith wrote:
> 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.


jean-michel bain-cornu 03-03-2006 02:33 PM

Re: how to write file with cp1250 encodings?
 
Hi,
Grzegorz Smith wrote:
> 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


All times are GMT. The time now is 01:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.