sqlite - how to remove u from sqlite3 cursor.fetchall() in python -
import sqlite3 class class1: def __init__(self): self.print1() def print1(self): con = sqlite3.connect('mydb.sqlite') cur = con.cursor() cur.execute("select fname tblsample1 order fname") ar=cur.fetchall() print(ar) class1()
gives output shown below
[(u'arun',), (u'kiran',), (u'kulku',), (u'sugu',)]
i need remove u array , need output shown below
['arun', 'kiran', 'kulku', 'sugu']
if need strings retrieved sqlite database returned utf-8 instead of unicode, set-up connection accordingly using text_factory propery:
import sqlite3 class class1: def __init__(self): self.print1() def print1(self): con = sqlite3.connect('mydb.sqlite') con.text_factory = str cur = con.cursor() cur.execute("select fname tblsample1 order fname") ar=cur.fetchall() print(ar) class1()
see details: http://docs.python.org/library/sqlite3.html#sqlite3.connection.text_factory
that takes care of "u" in front of strings. you'll have convert list of tuples list:
ar=[r[0] r in cur.fetchall()]
Comments
Post a Comment