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

Reply via email to