Errors When Pulling Information from CSV File to Python

Joined
Dec 10, 2020
Messages
1
Reaction score
0
I am trying to create a program which will calculate how much an artist will make on various streaming services. I want the streams to be user input and calculated by the data given in the CSV file. I keep getting key errors and invalid indexing and I'm unsure how to fix such errors
```
import csv

filename = "paymentperstream.csv"
with open(filename, 'r') as csvfile:
csvreader = csv.DictReader(csvfile) #reading file
fieldnamesVariable = "'Spotify', 'Apple Music', 'Google Play Music', 'Deezer', 'Pandora', 'Amazon Music Unlimited', 'Tidal'" #variable of the row names
rows = [" Spotify", "Apple Music", "Google Play Music", "Deezer", "Pandora", "Amazon Music Unlimited", "Tidal"] #list of the row names (doesnt do anything)

streams=int(input("How many streams would you like to calculate: ")) #user input
print("The options of streaming services are " + fieldnamesVariable) #gives user the options of streaming services they can choose from
platform=input("What platform would you like to calculate your streams on: (Case Sensitive! Please type name as it appears above) ") #user chooses service

if platform == "Spotify":
for row in csvreader:
print("Spotify will pay you $" + row["2"] + " per stream") #if the user chooses Spotify, take data from row 2 (the row with spotify in it) of the file
print("You would earn $" + streams*row["2"]) #multiplies "streams" by the data in row 2 of the file (DOES NOT WORK, HERE IS WHERE THE ERRORS OCCUR)

elif platform == "Apple Music":
for row in csvreader:
print("Apple Music will pay you $" + row["3"] + " per stream") #see above
print("You would earn $" + streams*row["3"])

elif platform == "Google Play Music":
for row in csvreader:
print("Google Play Music will pay you $" + row["4"] + " per stream")
print("You would earn $" + streams*row["4"])

elif platform == "Deezer":
for row in csvreader:
print("Deezer will pay you $" + row["5"] + " per stream")
print("You would earn $" + streams*row["5"])

elif platform == "Pandora":
for row in csvreader:
print("Pandora will pay you $" + row["6"] + " per stream")
print("You would earn $" + streams*row["6"])

elif platform == "Amazon Music Unlimited":
for row in csvreader:
print("Amazon Music Unlimited will pay you $" + row["7"] + " per stream")
print("You would earn $" + streams*row["7"])

elif platform == "Tidal":
for row in csvreader:
print("Tidal will pay you $" + row["8"] + " per stream")
print("You would earn $" + streams*row["8"])

else:
print("Invalid Input") #if they dont give the name of a streaming service
```
 

Attachments

  • error.jpg
    error.jpg
    54.7 KB · Views: 78
  • spotify payment.jpg
    spotify payment.jpg
    27.1 KB · Views: 80

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,057
Messages
2,570,443
Members
47,115
Latest member
DorothyLus

Latest Threads

Top