#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Problem to use the information from my database


    Hi!

    I have problem to use the information from my database.


    Code:
    #! /usr/bin/env python
    from xlwt import *
    import psycopg2
    
    
    try:
        conn = psycopg2.connect("dbname=statistics user=postgres host=xxx.xxx.xxx.xxx password=xxxxxx")
    except:
        print "Not connected to Database"
    cur = conn.cursor()        
    
    try:
        cur.execute("select * from \"siteinfo\" where dnit=1042 AND knappval=3")
    except:
        print "I can't SELECT from Database"
    rows = cur.fetchall()
    for row in rows:
        Namn = row[2]+" "+row[3]
    
    cur.close()
    conn.close()  
    print Namn
    
    book = Workbook(encoding='utf-8')
    sheet = book.add_sheet('Bemanning')
    sheet.write(1,2,Namn,easyxf())
    book.save(Namn+'.xls')
    print Namn prints : Kirurgmottagningen Karlshamn Rådgivning

    book.save(Namn+'.xls') saves the file as: Kirurgmottagningen Karlshamn RÃ¥dgivning.xls

    How shall i do to make the saved file be named Kirurgmottagningen Karlshamn Rådgivning.xls ??
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Get rid of the pain and install the Django framework for Python. Once installed, download a Django-ORM standalone app or create one and you're set. Now you can use Django's queryset instead of dirty SQL statements.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Pythong
    Get rid of the pain and install the Django framework for Python. Once installed, download a Django-ORM standalone app or create one and you're set. Now you can use Django's queryset instead of dirty SQL statements.

    The problem isnt really how i get the data from the databas.
    Its when i shall use it in the book.save and have swedish letter in it like "åäö".

    But you mean django vill help me out whit that? do django anything whit the selected information in the database so it understand and can you the data whit "åäö" in it?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Somehow two extra bytes were inserted.
    Code:
    >>> A='Rådgivning'
    >>> A
    'R\xc3\xa5dgivning'
    >>> print(A)
    Rådgivning
    >>> B='RÃ¥dgivning'
    >>> B
    'R\xc3\x83\xc2\xa5dgivning'
    >>> print(B)
    RÃ¥dgivning
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by Nibur
    print Namn prints : Kirurgmottagningen Karlshamn Rådgivning

    book.save(Namn+'.xls') saves the file as: Kirurgmottagningen Karlshamn RÃ¥dgivning.xls

    How shall i do to make the saved file be named Kirurgmottagningen Karlshamn Rådgivning.xls ??
    Python 2, I presume – Python 3 doesn’t have this kind of ****

    Try prepending string literals with “u” in order to get Unicode strings, as in:

    Code:
    book.save(namn + u'.xls')
    Sometimes this is enough, sometimes you get UnicodeDecodeErrors anyway, and you have to do multiple decodings and encodings as in:

    Code:
    namn = namn.decode('Latin-1').encode('UTF-8')
    (or something like it.) UTF-8 is typical for Linux; Windows might need UTF-16, I don’t know.

    Python 2’s Unicode support is an absolute mess.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Thank you all for helping me out.
    This is how the final code that sorted out my problem
    Code:
    #!/usr/bin/python
    # -*- coding: latin-1 -*-
    from xlwt import *
    import psycopg2
    
    
    try:
        conn = psycopg2.connect("dbname=statistics user=postgres host=xxx.xxx.xxx.xxx password=xxxx")
    except:
        print "Not connected to Database"
    cur = conn.cursor()        
    
    try:
        cur.execute("select * from \"siteinfo\" where dnit=1042 AND knappval=3")
    except:
        print "I can't SELECT from Database"
    rows = cur.fetchall()
    for row in rows:
        Namn = row[2]+" "+row[3]
    
    cur.close()
    conn.close()  
    print Namn
    print row[3]
    
    Namn = Namn.replace('å', '\xe5')
    Namn = Namn.replace('ä', '\xe4')
    Namn = Namn.replace('ö', '\xf6')
    Namn = Namn.replace('Å', '\xc5')
    Namn = Namn.replace('Ä', '\xc4')
    Namn = Namn.replace('Ö', '\xd6')
    
    
    
    
    book = Workbook(encoding='latin-1')
    sheet = book.add_sheet('Bemanning')
    sheet.write(1,2,Namn,easyxf())
    book.save(Namn+'.xls')

IMN logo majestic logo threadwatch logo seochat tools logo