"""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.Searchn2.Recommendn3.Editn4.Saven"))
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 Songsn2.Display All Artistsn3.Search By Artistn4.Display All Genresn5.Search by Genren6.Display All Playlistsn7.Search By Playlistn"))
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 Titlen2.Recommend by Artist Namen"))
if recommendselection == 1:
songrec = str(raw_input("Please enter the song titlen"))
elif recommendselection == 2:
artistrec = str(raw_input("Please enter the Artist's namen"))
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 Songn2.Create New Playlistn3.Add a song to a current playlist"))
if editselection == 1:
songadd = str(raw_input("Please enter the EVERYTHINGn"))
elif editselection == 2:
playistcreate = str(raw_input("Please enter the name of the Playlistn"))
elif editselection == 3:
playlistadd = str(raw_input("Please enter the name of the playlistn"))
break
elif selection == 4:
print "You selected Save."
f.close()
break
这就是我目前所拥有的。这是一个正在进行的 python 项目,现在我被难住了;我正在尝试按艺术家搜索,例如如果用户将贾斯汀汀布莱克输入为"艺术家搜索",那么我希望提取索引,以便我可以匹配歌曲列表中的索引并向用户显示该信息。
任何帮助确定为什么贾斯汀汀布莱克不是列表中的值,即使当我运行显示所有艺术家选项时显示名称,将不胜感激。
为匹配列表索引指明正确的方向也很棒。这是曲调的一个例子.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
我认为您应该为要存储的数据创建一个特定的类,然后创建一个实例化此类的对象列表:
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)