Jul-21-2023, 05:21 PM
(Jul-21-2023, 04:21 PM)deanhystad Wrote: I suggest you start with this:
import csv with open("harrypotter.csv") as file: reader = csv.reader(file) for student in reader: print(student)It will point out the reason for your unpacking problem.
You should never try to write a complete program. Write your program in parts. Get one part working and then move on to the next. For this program I would first verify I can read the CSV file. Once I do that, I would work on the part that adds the students to the sutdents list. Finally I would work on the part that produces output.
Thank you for the response and for the note posted!
So after running the code given to me the Terminal outputs the following.
Output:['Harry', ' "Number Four', ' Privent Drive"']
['Ron', ' The Burrow']
['Draco', ' Malfoy Manor']
Which format wise it doesn't look the best but it works.Then I ran following code testing the appen function
import csv students = [] with open("harrypotter.csv") as file: reader = csv.reader(file) for student in reader: students.append print(students)the terminal output for this was this
Output:[['Harry', ' "Number Four', ' Privent Drive"'], ['Ron', ' The Burrow'], ['Draco', ' Malfoy Manor']]
So appending is working fine.So I added the fact we are adding dictionaries to a list.
import csv students = [] with open("harrypotter.csv") as file: reader = csv.reader(file) for name, home in reader: students.append({"name": name, "home": home}) print(students)The Terminal is Outputed this error
Error:Traceback (most recent call last):
File "c:\Users\abarn\OneDrive\Documents\Desktop\Python Projects\harry.py", line 5, in <module>
for name, home in reader:
^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
So I am unsure why it suddenly has problem when I try to pass into two variables in the for loop. Any idea?