我有带id和codename colums的sqlite db表。
1 aaaa1223
2 bbbb2321321
3 cccc23112
我有txt文件:
aaaa Result1
bbbb Result2
cccc Result3
....
如何循环搜索功能?
现在我的结果仅打印:
aaaa = Result1
但我需要打印:
aaaa = Result1
bbbb = Result2
cccc = Result3
import sqlite3
import codecs
con = sqlite3.connect('database.dbl')
cur = con.cursor()
cur.execute("SELECT id,codename FROM data")
rows = cur.fetchall()
filetxt = codecs.open("codenamefile.txt", 'r', 'utf-8')
def ScanData(f):
for line in filetxt:
if f==line.split(" ")[0]:
print (codenamec+" = "+line.split(" ")[1])
return
for row in rows:
codename=row[1]
codenamec=codename[:4]
ScanData(codenamec)
答案 0 :(得分:0)
您可以将文件内容读入字典,然后将re
与数据库中的结果一起使用:
import sqlite3
file_data = dict(i.strip('\n').split() for i in open('filename.txt'))
data = sqlite3.connect('filename.db').cursor().execute('SELECT * FROM table')
final_results = dict((lambda x:[x, file_data[x]])(re.findall('[a-z]+', i)[0]) for _, i in data)
输出:
{'aaaa': 'Result1', 'bbbb': 'Result2', 'cccc': 'Result3'}