473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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
Feb 27 '06 #1
3 6145
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'Ldz'
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

Feb 27 '06 #2
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.

Mar 1 '06 #3
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
Mar 3 '06 #4

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

Similar topics

4
by: Achim Domma | last post by:
Hi, I read some text from a utf-8 encoded text file like this: text = codecs.open('example.txt','r','utf8').read() If I pass this text to a COM object, I can see that there is still the BOM...
14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
4
by: hagai26 | last post by:
I have a hebrew text file, which I want to read in python I don't know which encoding I need to use & how I do that thanks, hagai
3
by: john | last post by:
I need to produce a RTF-document which is filled with data from a database. I've created a RTF-document in WordPad (a template, so to speak) which contains 'placeholders', for example '<dd01>',...
29
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt,...
7
by: amygdala | last post by:
Hi, I'm trying to let PHP write a 'sitemap.xml' sitemap for Google and other searchengines. It's working, except that the content in the XML file doesn't seem to be UTF8. (Which it should be,...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
0
by: sweavo | last post by:
Hi all, (Python 2.5 under cygwin) I'm reading a bunch of XML files and merging them in memory - each file contains a number of packages which are to be merged: for fname in gInfiles: ...
4
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Š,Đ,Ž,Č,Ć). How can I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.