how to write file with cp1250 encodings?

Waldemar Osuch waldemar.osuch at gmail.com
Mon Feb 27 00:25:19 EST 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




More information about the Python-list mailing list