Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 15th, 2013, 05:47 AM
Nibur Nibur is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Nibur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 34 m 44 sec
Reputation 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 ??

Reply With Quote
  #2  
Old February 15th, 2013, 01:07 PM
Pythong Pythong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Pythong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 12 sec
Reputation 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.

Reply With Quote
  #3  
Old February 18th, 2013, 02:16 AM
Nibur Nibur is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Nibur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 34 m 44 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old February 18th, 2013, 06:57 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
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!

Reply With Quote
  #5  
Old February 18th, 2013, 07:09 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 h 56 m 16 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #6  
Old February 19th, 2013, 02:51 AM
Nibur Nibur is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Nibur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 34 m 44 sec
Reputation 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')

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problem to use the information from my database

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap