The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Problem to use the information from my database
Discuss Problem to use the information from my database in the Python Programming forum on Dev Shed. Problem to use the information from my database Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 15th, 2013, 05:47 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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 ??
|

February 15th, 2013, 01:07 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
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.
|

February 18th, 2013, 02:16 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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?
|

February 18th, 2013, 06:57 AM
|
 |
Contributing User
|
|
|
|
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!
|

February 18th, 2013, 07:09 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
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)
|

February 19th, 2013, 02:51 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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')
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|