如何正确分割这些歌曲细节细节

时间:2018-06-03 19:06:20

标签: python

这可能没有意义,但我有一个文件,包括(每行)歌曲名称,歌曲长度,艺术家,专辑和流派。所以,我尝试用空格分割它们,当然这会分割歌曲标题,所以我只是根据另一个计算使用列表的第一个whatevers。我的例子是:

Hells Bells     5:13    AC/DC   Back In Black   Rock    0   6   
Shoot to Thrill     5:18    AC/DC   Back In Black   Rock    0   6   
Back in Black       4:16    AC/DC   Back in Black   Rock    0   6   

所以我有一个for循环,它计算了那些分裂的列表的长度,它给了我想要的东西。

    for line in f:
        Thing = line.replace(' ', ',').replace('\t', ',')
        a = Thing.split(',')
        integar = len(a)-11 #This subtract 11 is to get just the song which doesn't work situationally because if the album has more than 3 words unlike in this situation, then it wouldn't give the right amount
        integar = integar+1
        for i in range(integar):
            print(a[i+1])
            namelist.append(a[i])
        name = str(namelist)
        namelist=[]

我很好奇我将如何获得它的标题。我想它有可能遍历列表并找到长度,直到找到一个数字,但我不确定如何做到这一点,或者是否有更好的解决方案。

1 个答案:

答案 0 :(得分:0)

我将你的歌曲线粘贴到一个文本编辑器中(我认为它们可能被标签分隔),但它就像是随机的4或3个空格 - 如果你只是想要分开的话,这可能就是一个可能的解决方案信息:

namedir = {}

for line in f:
    line = line.split("    ")   # 4 spaces, returns list
    name = line[0]
    duration = line[1].replace(' ', '')
    line[2] = line[2].split("   ")  # 3 spaces, no idea why ??
    band = line[2][0]
    album = line[2][1]
    genre = line[2][2]
    meta = line[-1]

    namedir[name] = (duration, band, album, genre, meta)

for song in namedir.keys():
    print(song, ", ".join(namedir[song]))

我在运行时将其作为输出

$ python t1.py 
('Shoot to Thrill', '5:18, AC/DC, Back In Black, Rock, 0   6')
('Hells Bells', '5:13, AC/DC, Back In Black, Rock, 0   6')
('Back in Black', '4:16, AC/DC, Back in Black, Rock, 0   6')

希望这会有所帮助