lundi 7 décembre 2015

Can't read file after split line?

file = open("songs.txt" , "r")
songs = {}

for line in file:
    (song, name) = line.split(",")
    songs[song] = str(name)

print(songs)

songs = {}

print("Welcome to the Virtual Playlist!")

while True:
    print("")
    print("What would you like to do?")
    print("1. View the playlist")
    print("2. View playlist sorted by artist name")
    print("3. View playlist sorted by song name")
    print("5. Add a song")
    print("6. Exit")
    choice = input("").strip()

    if choice == "1":
        print(file.read())

I am trying to read from a file then output everything in it, it works fine if I exclude the part where it reads every line and stores it into a dictionary. But it doesn't work if that part is included. Why is this happening?

Also, when it stores the songs and artists into a dictionary, it includes /n in some of the entries since there a space there in the file. Is there any way to exclude that /n thing because I just wan't the song name/artist.

Aucun commentaire:

Enregistrer un commentaire