从sqli中提取单个值

2024-05-15 00:00:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在sqlite3db中有一个列。此列包含单个条目的一个或多个值。在

src

我说的是tags列。 我要做的是将它们提取为单个元素。 所以我开发了这个代码:

import sqlite3

db = sqlite3.connect('db\\archivio')
db.row_factory = lambda cursor, row: row[0]
c = db.cursor()

tags = c.execute("SELECT tags from documenti").fetchall()

lista_tags = []

for i in tags:
    lista_tags.append(i)

#user_tag = input('Insert tag to search for: ')

for val in lista_tags:
    print(val)

结果是:

^{pr2}$

问题是我不能把它们一个一个地添加到列表中。 如果打印lista_tags,则输出如下:

['hansel, gretel', 'cappuccetto, rosso, lupo', 'signore, anelli, gollum', 'blade,runner', 'pinocchio', 'incredibili']

如何将这组值添加到列表中的单个值中?在

['hansel', 'gretel', 'cappuccetto', 'rosso', 'lupo', 'signore', 'anelli', 'gollum', 'blade','runner', 'pinocchio', 'incredibili']

Tags: in列表fordbtagtagsvalsqlite3
2条回答

List is a collection of value so you are append value in lista_tags so you get all value

for i in tags: lista_tags.append(i)

if you want to get single value by accessing them using "key" lista_tags[1] it will give you single value

假设您的tags = ['hansel, gretel', 'cappuccetto, rosso, lupo', 'signore, anelli, gollum', 'blade,runner', 'pinocchio', 'incredibili']在您的fetchall()之后,您可以通过以下方式实现您的结果:

lista_tags = []

for entry in tags:
    lista_tags += entry.split(",")

输出将是:

^{pr2}$

相关问题 更多 >

    热门问题