New to python. So I am running this script, where for an mp3 file is heard(listening api) and the script return the metadata(artist,track name) for that file, with varying confidence percentages.
Code snippet:
first = True
for score, rid, title, artist in results:
if first:
first = False
else:
print()
cursor.execute("update sc_download set Artist_name=%s , Song_name=%s , MB_ID=%s , Match_Percent=%s where SC_TID=%s" , (artist.encode('utf-8'), title.encode('utf-8'), rid, score*100, track_id))
conn.commit()
print('%s\n%s' % (artist, title))
print('%s' % rid)
print('%i%%' % (int(score*100)))
The problem is, I have around 3-4 outputs, with the highest percentage being around 90% and the lowest being 50%. I am also, inserting this data in mysql, and by default, the last output(with lower percentage) is being written.
The output :
Lana Del Rey Summertime Sadness (Cedric Gervais remix) f85d4c4d-20e0-4cdc-a443-45fd5eaeffdc 91%
Lana Del Rey Summertime Sadness (Cedric Gervais remix) 14aa03d7-3923-45df-b0e3-8ff72b94fc10 69%
Any way to capture only the first output and insert that into the databse?
Aplogies for any lack of clarity, unintentional.
(1) For simplicity, get rid of that if at the top of your loop. Move the print statement to the line before the loop.
(2) If you want only the one result, then don't loop through all of them. Grab the first and deal with that one alone. Specifically, trade in your for command for
score, rid, title, artist = results[0]
Then continue with the rest of your code.