如何从列表中的字符串中删除数字并再次检索它们

时间:2014-11-28 19:22:12

标签: python python-3.3

好的,这里是场景: 我正在制作一个Windows 7程序,用于搜索目录中的所有音乐文件,并根据该列表检查用户的输入,以便它可以播放用户选择的歌曲。因为曲目'名称通常编号如下:' 01 name.mp3',我想删除' 01'部分来自新列表并让程序记录' 01',以便它可以从新列表中识别它并启动文件。换句话说,我希望程序创建一个没有数字的新文件名列表,让程序识别文件并通过知道数字启动它。这可能吗? (告诉我,如果这没有任何意义。) 另外,这是我的一些代码:

def GetMediaFName(source, name):
            for key, val in source.iteritems():
                if val == name:
                    return key

newList = {}     
media_list = []
for dirpath, dirnames, filenames in os.walk(music_dir) and os.walk(alt_music_dir1):
    for filename in [f for f in filenames if f.endswith(".m4a") or f.endswith(".mp3") or f.endswith(".mp4")]:

        media_list.append(os.path.join(dirpath, filename))



        media_from_menu = menu[5:]
        print(media_from_menu)
        media_to_search1 = media_from_menu + ".mp3"
        media_to_search2 = media_from_menu + ".wav"
        media_to_search3 = media_from_menu + ".m4a"
        media_to_search4 = media_from_menu + ".mp4"

        for i in media_list:
            spl = i.split('\\')[-1]
            if spl is not None:
                try:
                    tmp = re.findall(r'\d+',spl.split('.')[0][0])
                    newList.update({i:i.replace(tmp,"").strip()})
                except Exception:
                    newList.update({i:spl})

        itms = newList.keys()

            for i in files:
                tmp = re.findall(r'\d+',i.split('.')[0][0])
                newList.update({i:i.replace(tmp,"").strip()})
            print(newList)

        print(GetMediaFName(newList, media_from_menu + ".mp3"))

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解正确,但您希望跟踪原始文件名,同时向用户显示不同的名称(假设没有索引编号的曲目名称)。

我认为这可能会让你对它有所了解。你打算使用一些GUI库吗?

import re

def getFileName(source,name):
    for key,val in source.iteritems():
        if val == name:
            return key

names = ['01 name_a.mp3','02 name_b.mp3','03 name_c.mp3','04 name_d.mp3']

newList = {}

for i in names:
    tmp = re.findall(r'\d+',i.split('.')[0])[0]
    newList.update({i:i.replace(tmp,"").strip()})

print newList

# If names are not the same, but you run in trouble if all of tracks are 01 name.mp3 , 02 name.mp3 and so on

print getFileName(newList,'name_a.mp3')

# Another possible way ! get the index of element user has clicked on and pass it to a list of original file names
user_selected = 2
itms = newList.keys()
print itms[user_selected]

编辑:

在某个路径中查找Mp3,包括其子目录中的文件:

import os
import os.path
import re

def getFileName(source,name):
    for key,val in source.iteritems():
        if val == name:
            return key


names = []
# From : http://stackoverflow.com/a/954522/3815839
for dirpath, dirnames, filenames in os.walk("C:\Users\test\Desktop\mp3s"):
    for filename in [f for f in filenames if f.endswith(".mp3")]:
        names.append(os.path.join(dirpath, filename))

newList = {}
for i in names:
    spl = i.split('\\')[-1]
    if spl is not None:
        try:
            tmp = re.findall(r'\d+',spl.split('.')[0])[0]
            newList.update({i:i.replace(tmp,"").strip()})
        except Exception:
            newList.update({i:spl})
itms = newList.keys()
print newList

print getFileName(newList,'name_a.mp3')