2017-03-28 9 views
0
"""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プロジェクトですが、今は私が困惑しています。ジャスティン・ティンバーレイクがユーザーによって「artistsearch」と入力された場合のように、アーティストが検索しようとしています。インデックスをプルダウンして、曲リストのインデックスと一致させてその情報をユーザーに表示できるようにします。Pythonアシスタンスリストを検索する

私がディスプレイを実行したときに名前が表示されても、ジャス​​ティン・ティンバーレイクがリストの価値でない理由を判断するのに役立つものは、すべてのアーティストオプションを高く評価します。

また、リストインデックスを一致させるために正しい方向を指し示すことも同様に優れています。これは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 
+0

適切なデータ構造である 'list'のようには聞こえません。これは辞書/ネストされた辞書で大幅に簡略化されます。 –

+0

あなたは正しいかもしれませんが、私はこれまでずっとこれまで来ており、辞書のデータ構造を実装するためにどれだけの追跡と研究が必要かと疑問に思います。 – Sam

+0

https://en.wikipedia.org/wiki/Technical_debt –

答えて

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)