Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to write file with cp1250 encodings?

Reply
Thread Tools

how to write file with cp1250 encodings?

 
 
Grzegorz Smith
Guest
Posts: n/a
 
      02-27-2006
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
 
Reply With Quote
 
 
 
 
Waldemar Osuch
Guest
Posts: n/a
 
      02-27-2006
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

 
Reply With Quote
 
 
 
 
Magnus Lycka
Guest
Posts: n/a
 
      03-01-2006
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.

 
Reply With Quote
 
jean-michel bain-cornu
Guest
Posts: n/a
 
      03-03-2006
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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
is better to open, write, close file than open, write, append, close? Iulian Ilea Javascript 1 12-21-2006 04:32 PM



Advertisments