Python协助搜索列表

时间:2017-03-28 20:27:38

标签: python database list indexing

"""read file and store into database"""
f = open('C:\\Users\\user.name\\Desktop\\tunes.txt','r')
artist=[""]
song=[""]
album=[""]
genre=[""]
index=0
for line in f:
    if index==0:
        artist.append(line)
        index=index+1
    elif index==1:
        song.append(line)
        index=index+1
    elif index==2:
        album.append(line)
        index=index+1
    elif index==3:
        genre.append(line)
        index=index+1
    elif index==4:
        index=0   
while 1:
    selection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Search\n2.Recommend\n3.Edit\n4.Save\n"))
    if selection == 1:
        print "You selected Search"
        searchselection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Display All Songs\n2.Display All Artists\n3.Search By Artist\n4.Display All Genres\n5.Search by Genre\n6.Display All Playlists\n7.Search By Playlist\n"))                               
        if searchselection == 1:
           print '[%s]' % ''.join(map(str, song))
        elif searchselection == 2:
            print '[%s]' % ''.join(map(str, artist))
        elif searchselection == 3:
            artistsearch = str(raw_input("\nWhat artist are you searching for?\n"))
            artist.index(artistsearch)
            print value
        elif searchselection == 4:
            print '[%s]' % ''.join(map(str, genre))
        elif searchselection == 5:
            print "display"
        elif searchselection == 6:
            print "display"
        elif searchselection == 7:
            print "display"
        break
    elif selection == 2:
        print "You selected recommend."
        recommendselection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Recommend by Song Title\n2.Recommend by Artist Name\n"))
        if recommendselection == 1:
            songrec = str(raw_input("Please enter the song title\n"))

        elif recommendselection == 2:
            artistrec = str(raw_input("Please enter the Artist's name\n"))
        break
    elif selection == 3:
        print "You selected edit."
        editselection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Add a New Song\n2.Create New Playlist\n3.Add a song to a current playlist"))
        if editselection == 1:
            songadd = str(raw_input("Please enter the EVERYTHING\n"))
        elif editselection == 2:
            playistcreate = str(raw_input("Please enter the name of the Playlist\n"))
        elif editselection == 3:
            playlistadd = str(raw_input("Please enter the name of the playlist\n"))      
        break
    elif selection == 4:
        print "You selected Save."
        f.close()
        break

这就是我到目前为止所拥有的。这是一个正在进行的python项目,现在我很难过;我试图按艺术家搜索,如果Justin Timberlake被用户输入为“artistsearch”,那么我希望拉出索引,以便我可以匹配歌曲列表中的索引并将该信息显示给用户。

任何帮助确定为什么Justin Timberlake不是列表中的值,即使在我运行显示器时显示的名称所有艺术家选项都将非常感谢。

同样指向匹配列表索引的正确方向也是很好的。这是tunes.txt的一个例子:

Alicia Keys
No One
As I Am
R&B/Soul;Syl tunes

Everything But the Girl
Missing
Amplified Heart
Alternative;Syl tunes

Gathering Field
Lost In America
Lost In America
Rock

Ellie Goulding
Burn
Halcyon
Pop;Road tunes

Justin Timberlake
Mirrors
Mirrors (Radio Edit) - Single
Pop;Syl tunes

1 个答案:

答案 0 :(得分:0)

我认为你应该为你想要存储的数据创建一个特定的类,然后创建一个实例化这样的类的对象列表:

class Song():
    """Class to store the attributes for each song"""
    def __init__ (self):
        self.artist = ""
        self.song = ""
        self.album = ""
        self.genre = ""

# List to store all the Song objects
songs_list = []

f = open('C:\\Users\\user.name\\Desktop\\tunes.txt','r')
index=0
for line in f:
    # Instantiate an empty Song object
    s = Song()
    if index == 0:
        s.artist = line
        index=index+1
    elif index == 1:
        s.song = line
        index = index + 1
    elif index == 2:
        s.album = line
        index = index + 1
    elif index == 3:
        s.genre = line
        index = index+1
    elif index == 4:
        index = 0
    songs_list.append(s)