我想从MySQL数据库中选择一些行并保存在一个列表中,然后与变量值进行比较,但结果为空
这是我的代码:
mycursor = lucas_db.cursor()
mycursor.execute("SELECT Release_Name FROM Lucas_Table WHERE Published_Time > SUBDATE( CURRENT_TIME, INTERVAL 30 MINUTE)")
myresult = mycursor.fetchall()
print(myresult)
last=input('give me:')
me = difflib.get_close_matches(last, myresult)
print(me)结果是:
[('Food-Fact or Fiction S04E16 Tea Time WEBRip x264-CAFFEiNE',), ('A Million Little Things S01E17 720p HEVC x265-MeGusta',), ('The Pioneer Woman S21E09 16-Minute Chicken 480p x264-mSD',), ('The Pioneer Woman S21E09 16-Minute Chicken AAC MP4-Mobile',), ('Northern Rescue S01E09 720p HEVC x265-MeGusta',), ('Food-Fact or Fiction S04E16 Tea Time XviD-AFG',), ('Food-Fact or Fiction S04E16 Tea Time 480p x264-mSD',), ('Food-Fact or Fiction S04E16 Tea Time AAC MP4-Mobile',), ('How to Get Away with Murder S05E15 720p HEVC x265-MeGusta',), ('The Titan Games S01E09 720p HEVC x265-MeGusta',)]
give me:The Pioneer Woman S21E09 16-Minute Chicken 480p x264-mSD
[]
***Repl Closed***发布于 2019-03-02 05:44:36
myresult将是一个元组列表,其中包含一个元素,即数据库中的单个选择列。
下面说明了difflib是怎么回事。
a = [('foo',),('bar',)]
difflib.get_close_matches('foo', a)
[]
a = [a[0] for a in a]
difflib.get_close_matches('foo', a)
['foo']发布于 2019-03-02 06:11:14
您可以使用chain.from_iterable()链接myresult中的所有字符串。您还可以尝试更改difflib.get_close_matches()中的cutoff参数
from itertools import chain
import difflib
myresult = [('Food-Fact or Fiction S04E16 Tea Time WEBRip x264-CAFFEiNE',), ('A Million Little Things S01E17 720p HEVC x265-MeGusta',), ('The Pioneer Woman S21E09 16-Minute Chicken 480p x264-mSD',), ('The Pioneer Woman S21E09 16-Minute Chicken AAC MP4-Mobile',), ('Northern Rescue S01E09 720p HEVC x265-MeGusta',), ('Food-Fact or Fiction S04E16 Tea Time XviD-AFG',), ('Food-Fact or Fiction S04E16 Tea Time 480p x264-mSD',), ('Food-Fact or Fiction S04E16 Tea Time AAC MP4-Mobile',), ('How to Get Away with Murder S05E15 720p HEVC x265-MeGusta',), ('The Titan Games S01E09 720p HEVC x265-MeGusta',)]
c = chain.from_iterable(myresult)
print(difflib.get_close_matches('Food', c, cutoff=0.1))输出:
['Food-Fact or Fiction S04E16 Tea Time XviD-AFG', 'Food-Fact or Fiction S04E16 Tea Time 480p x264-mSD', 'Food-Fact or Fiction S04E16 Tea Time AAC MP4-Mobile']发布于 2019-03-02 06:11:40
感谢@Rich Andrews,它成功了:
mycursor.execute("SELECT Release_Name FROM Lucas_Table WHERE Published_Time > SUBDATE( CURRENT_TIME, INTERVAL 30 MINUTE)")
myresult = mycursor.fetchall()
myresult = [a[0] for a in myresult]
last=input('give me:')
me = difflib.get_close_matches(last, myresult)
print(me)"James Martins Great British Adventure S01E03 WebX264-LiGATE“的结果是:
give me:James Martins Great British Adventure S01E03 WEB x264-LiGATE
['James Martins Great British Adventure S01E09 WEB x264-LiGATE', 'James Martins Great British Adventure S01E11 WEB x264-LiGATE']
***Repl Closed***https://stackoverflow.com/questions/54952635
复制相似问题