how to remove u from sqlite3 cursor.fetchall() in python

后端 未结 4 1060
情歌与酒
情歌与酒 2021-02-13 04:19
import sqlite3
class class1:
def __init__(self):
    self.print1()
def print1(self):
    con = sqlite3.connect(\'mydb.sqlite\')
    cur = con.cursor()
    cur.execute(\"         


        
4条回答
  •  迷失自我
    2021-02-13 05:02

    If you need that strings retrieved from your sqlite database be returned as UTF-8 instead of Unicode, set-up your connection accordingly using the 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 from tblsample1 order by fname")
        ar=cur.fetchall()
        print(ar)
    
    class1()
    

    See this for the details: http://docs.python.org/library/sqlite3.html#sqlite3.Connection.text_factory

    That takes care of the "u" in front of your strings. You'll then have to convert your list of tuples to a list:

    ar=[r[0] for r in cur.fetchall()]
    

提交回复
热议问题