how to write file with cp1250 encodings?

Discussion in 'Python' started by Grzegorz Smith, Feb 27, 2006.

  1. 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
     
    Grzegorz Smith, Feb 27, 2006
    #1
    1. Advertising

  2. 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
     
    Waldemar Osuch, Feb 27, 2006
    #2
    1. Advertising

  3. Grzegorz Smith

    Magnus Lycka Guest

    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.
     
    Magnus Lycka, Mar 1, 2006
    #3
  4. 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
     
    jean-michel bain-cornu, Mar 3, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Caterpillar

    fsm state encodings

    Caterpillar, Feb 10, 2006, in forum: VHDL
    Replies:
    1
    Views:
    603
  2. Per Bolmstedt
    Replies:
    0
    Views:
    442
    Per Bolmstedt
    Mar 3, 2004
  3. Jaroslav Dobrek

    read from file with mixed encodings in Python3

    Jaroslav Dobrek, Nov 7, 2011, in forum: Python
    Replies:
    2
    Views:
    266
    Peter Otten
    Nov 7, 2011
  4. Ilya Zakharevich

    Read/write with UCS-2* encodings - Possible???

    Ilya Zakharevich, Feb 17, 2009, in forum: Perl Misc
    Replies:
    7
    Views:
    416
    Ilya Zakharevich
    Feb 20, 2009
  5. Roedy Green

    A proposal to handle file encodings

    Roedy Green, Nov 22, 2012, in forum: Java
    Replies:
    31
    Views:
    952
    Peter J. Holzer
    Dec 2, 2012
Loading...

Share This Page