遍历两个列表,检查是否匹配,然后重命名 [英] Iterate through two lists, check for matches and then rename

查看:72
本文介绍了遍历两个列表,检查是否匹配,然后重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有两个列表;一个是歌曲标题列表,另一个是通过运行os.listdir()生成的文件列表,这些文件将是歌曲mp3文件.

Ok, so I have two lists; one is a list of song titles, the other is a list of files that is generated by running os.listdir(), which will be song mp3 files.

已更新

songs = ['The Prediction', 'Life We Chose', 'Nastradamus', 'Some of Us Have Angels', 'Project Windows', 'Come Get Me', "Shoot 'em Up", 'Last Words', 'Family', 'God Love Us', 'Quiet Niggas', 'Big Girl', 'New World', 'You Owe Me', 'The Outcome']

每首歌都是unicode

Each song is unicode

filenames = ['Nas - Big Girl.mp3', 'Nas - Come Get Me.mp3', 'Nas - God Love Us.mp3', 'Nas - Life We Chose.mp3', 'Nas - Nastradamus.mp3', 'Nas - New World.mp3', "Nas - Shoot 'Em Up.mp3", 'Nas - Some of Us Have Angels.mp3', 'Nas - The Outcome.mp3', 'Nas - The Prediction.mp3', 'Nas Feat. Bravehearts - Quiet Niggas.mp3', 'Nas Feat. Ginuwine - You Owe Me.mp3', 'Nas Feat. Mobb Deep - Family.mp3', 'Nas Feat. Nashawn - Last Words.mp3', 'Nas Feat. Ronald Isley - Project Windows.mp3']

每个文件名是一个字符串

Each filename is a string

我希望能够查看歌曲列表,如果歌曲列表中的一项与文件名列表中的内容匹配,请将该文件重命名为歌曲的文件.

I want to be able to look at the songs list, if one of the items from the songs list matches inside the filenames list, rename the file to that of the song.

这有意义吗?

推荐答案

基本上看起来像这样:

import os

for song in songs:
    for filename in filenames:
        if song.lower() in filename.lower():  # lower() just in case
            os.rename(filename, song + '.mp3')

如果您还需要其他任何内容,请询问.

If you need anything else, please ask.

这篇关于遍历两个列表,检查是否匹配,然后重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆