Re: [Tutor] encode unicode strings from pysqlite

2008-04-14 Thread Dinesh B Vadhia
Hi! Kent.  The row[0].encode('utf-8') works perfectly within a standalone 
program.  But didn't work within webpy until I realized that maybe webpy is 
storing the row as a dictionary (which it does) and that you have to get the 
string by the key (ie. 'fieldB').  That worked and also webpy encodes the 
unicode string at the same time.  Here are the details:

# standard Python: testDB.py
con = sqlite3.connect("testDB.db")
cur = con.cursor()
query = "SELECT fieldB FROM testDB 
WHERE fieldB LIKE '%s' 
LIMIT '%s'" %(q, limit)
for row in cur.execute(query):# row is a list
print row[0].encode('utf-8')# works perfectly!

# webpy: testDB2.py
web.config.db_parameters = dict(dbn='sqlite', db="testDB.db")
for row in web.select('testDB', 
what='fieldB', 
where='fieldB LIKE $q', 
limit=limit, 
vars={'q':q}):
r = row['fieldB']# get encode'd unicode through 
dict key value
print r   # works perfectly!




- Original Message - 
From: Kent Johnson 
To: Dinesh B Vadhia 
Cc: tutor@python.org 
Sent: Monday, April 14, 2008 3:42 AM
Subject: Re: [Tutor] encode unicode strings from pysqlite


Dinesh B Vadhia wrote:
> Here is a program that SELECT's from a pysqlite database table and 
> encode's the returned unicode strings:

> query = "SELECT fieldB FROM testDB WHERE fieldB LIKE '%s' LIMIT '%s'" 
> %(q, limit)
> for row in cur.execute(query):

Here row is a list containing a single unicode string. When you convert 
a list to a string, it converts the list elements to strings using the 
repr() function. The repr() of a unicode string includes the u'' as part 
of the result.

In [64]: row = [u'99 Cycling Swords']
In [65]: str(row)
Out[65]: "[u'99 Cycling Swords']"

Notice that the above is a string that includes u' as part of the string.

What you need to do is pick out the actual data and encode just that to 
a string.
In [62]: row[0].encode('utf-8')
Out[62]: '99 Cycling Swords'

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] encode unicode strings from pysqlite

2008-04-14 Thread Kent Johnson
Dinesh B Vadhia wrote:
> Here is a program that SELECT's from a pysqlite database table and 
> encode's the returned unicode strings:

> query = "SELECT fieldB FROM testDB WHERE fieldB LIKE '%s' LIMIT '%s'" 
> %(q, limit)
> for row in cur.execute(query):

Here row is a list containing a single unicode string. When you convert 
a list to a string, it converts the list elements to strings using the 
repr() function. The repr() of a unicode string includes the u'' as part 
of the result.

In [64]: row = [u'99 Cycling Swords']
In [65]: str(row)
Out[65]: "[u'99 Cycling Swords']"

Notice that the above is a string that includes u' as part of the string.

What you need to do is pick out the actual data and encode just that to 
a string.
In [62]: row[0].encode('utf-8')
Out[62]: '99 Cycling Swords'

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] encode unicode strings from pysqlite

2008-04-14 Thread Dinesh B Vadhia
Here is a program that SELECT's from a pysqlite database table and encode's the 
returned unicode strings:

import sys
import os
import sqlite3

con = sqlite3.connect("testDB.db")
cur = con.cursor()

a = u'99 Cycling Swords'
b = a.encode('utf-8')
print b

q = '%wor%'
limit = 25
query = "SELECT fieldB FROM testDB WHERE fieldB LIKE '%s' LIMIT '%s'" %(q, 
limit)
for row in cur.execute(query):
r = str(row)
print r.encode('utf-8')


The print b results in: 99 Cycling Swords ... which is what I want.

But, the print r.encode('utf-8') leaves the strings as unicode strings eg. u'99 
Cycling Swords'

Any ideas what might be going on?

Dinesh




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor