简体   繁体   English

遍历两个列表,检查匹配项,然后重命名

[英]Iterate through two lists, check for matches and then rename

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.一个是歌曲名称列表,另一个是通过运行os.listdir()生成的文件列表,这将是歌曲 mp3 文件。

UPDATED更新

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']

Each song is unicode每首歌都是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.我希望能够查看歌曲列表,如果歌曲列表中的一项与文件名列表中的匹配,则将文件重命名为歌曲的文件名。

Does that make sense?那有意义吗?

Basically it looks like this:基本上它看起来像这样:

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.如果您还需要什么,请询问。

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

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