Python Forum
New to Python - Not sure why this code isn't working - Any help appreciated
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python - Not sure why this code isn't working - Any help appreciated
#3
(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?
Reply


Messages In This Thread
RE: New to Python - Not sure why this code isn't working - Any help appreciated - by TheGreatNinx - Jul-21-2023, 05:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Any help is appreciated! Butch12 3 883 Jul-22-2023, 03:23 AM
Last Post: Larz60+
  code not working when executed from flask app ThomasDC 1 1,120 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 2,090 May-09-2023, 08:47 AM
Last Post: buran
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,171 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,233 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 2,127 Mar-21-2022, 10:12 AM
Last Post: End3r
  interloper help appreciated combining two python3 scripts to add new conditions. Ricktoddfrank 0 1,551 Jun-22-2021, 10:24 PM
Last Post: Ricktoddfrank
  I don't undestand why my code isn't working. RuyCab 2 2,091 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 3,097 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,966 Feb-09-2021, 07:49 PM
Last Post: Patricia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020