我有一个SQLite数据库,我希望在使用Python时找到某个关键字并显示仅在某行中包含它们的数据库行:
import sqlite3 as lite
con = lite.connect("corpus.db")
c = con.cursor()
keyword = str(input("Which keyword do you wish to find in the corpus? "))
c.execute("SELECT Content FROM Corpus")
#I'm guessing there should be more parameters here
我对编码一般都很新,所以如果整件事看起来很吵,那就很抱歉。我似乎无法找到任何类似的解决方案。我尝试过这样做(尽管它并不完全是我正在寻找的,但我认为我至少可以解决以后的结果):
import sqlite3 as lite
con = lite.connect("corpus.db")
c = con.cursor()
keyword = str(input("Which keyword do you wish to find in the corpus? "))
#For example, "camera"
c.execute("SELECT Content FROM Corpus")
#In my case, content contains a list with this data [('A Japanese woman has been reunited with a camera she dropped in the ocean two-and-a-half years ago, after schoolchildren in Taiwan found it washed up on a beach and covered in barnacles.',), ('The writer is a Washington DC-based foreign affairs analyst. His views are his own.',), ...]
content = c.fetchall()
for text in content:
if keyword in text:
print(text)
所以,理论上,它应该打印一个关于日本女人的句子,但它什么都不打印,所以我不知道我应该做些什么来实际找到列表中的元素。它适用于此example。
答案 0 :(得分:1)
就我而言,内容包含一个包含此数据的列表 [('一名日本妇女已经与她掉在海里的相机重聚 两年半以前,在台湾的学龄儿童发现之后 在海滩上冲上来,用藤壶盖住。',),('作者是一个 华盛顿特区的外交事务分析师。他的观点是他的 拥有。',),...]
content
中返回的值似乎是tuples的列表
你必须在每个元组内再次迭代才能得到句子。
像这样:
for item in content:
# item = ('A Japanese woman has been reunited with a camera she dropped in the ocean two-and-a-half years ago, after schoolchildren in Taiwan found it washed up on a beach and covered in barnacles.',)
for sentence in item:
if keyword in sentence:
print(sentence)