繁体   English   中英

多种打印输出,仅打印其中一种

[英]Multiple print output, printing only one of them

python新手。 因此,我正在运行此脚本,其中会听到一个mp3文件(监听api),并且该脚本以不同的置信度百分比返回该文件的元数据(艺术家,曲目名称)。

程式码片段:

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)))

问题是,我大约有3-4个输出,最高百分比约为90%,最低百分比为50%。 我也是在mysql中插入此数据,默认情况下,正在写入最后一个输出(百分比较低)。

输出 :

拉娜·德尔·雷(Lana Del Rey)夏日悲伤(Cedric Gervais混音)f85d4c4d-20e0-4cdc-a443-45fd5eaeffdc 91%

Lana Del Rey Summertime Sadness(Cedric Gervais remix)14aa03d7-3923-45df-b0e3-8ff72b94fc10 69%

有什么办法只捕获第一个输出并将其插入数据库?

对于任何不明确,无意的道歉。

(1)为简单起见,如果您将其放在循环的顶部,请予以删除。 将print语句移到循环之前的行。

(2)如果只想要一个结果,那么不要遍历所有结果。 抢先一个,一个人处理。 具体来说,以您的for命令换取

score, rid, title, artist = results[0]

然后继续其余的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM