如何从python中的sqlite3 cursor.fetchall()中删除你 [英] how to remove u from sqlite3 cursor.fetchall() in python

查看:43
本文介绍了如何从python中的sqlite3 cursor.fetchall()中删除你的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import sqlite3
class class1:
def __init__(self):
    self.print1()
def print1(self):
    con = sqlite3.connect('mydb.sqlite')
    cur = con.cursor()
    cur.execute("select fname from tblsample1 order by fname")
    ar=cur.fetchall()
    print(ar)

class1()

给出如下所示的输出

[(u'arun',), (u'kiran',), (u'kulku',), (u'sugu',)]

我需要从数组中删除 u 并且我需要如下所示的输出

i need to remove u from the array and i need output as shown below

['arun', 'kiran', 'kulku', 'sugu']

推荐答案

如果您需要从 sqlite 数据库中检索到的字符串以 UTF-8 而不是 Unicode 格式返回,请使用 text_factory 属性相应地设置您的连接:

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()

查看详情:http://docs.python.org/library/sqlite3.html#sqlite3.Connection.text_factory

这会处理字符串前面的u".然后,您必须将元组列表转换为列表:

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()]

这篇关于如何从python中的sqlite3 cursor.fetchall()中删除你的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆