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
#1
Hi there! New to this forum and Python (Technically my first language I am learning to program in! :D) and I'm running into an issue that I don't quite understand.
So I've been watching youtuber video called Harvard CS50’s Introduction to Programming with Python – Full University Course on the channel freecodeCamp.org. During lecture at 8:09:01 we re-code what the previous example that he was explaining to make it easier to catch multiple columns in a CSV doc if it so happens to have any or some other format type. In the example we are using few Harry Potter Characters. Harry, Ron and Draco. End goal of this example is to pull data from a CSV doc that written like the following
Harry, "Number Four, Privent Drive"
Ron, The Burrow
Draco, Malfoy Manor

And to solve this what he suggest was us to import the library csv and use csv.reader to help us out with this to get the desire end result for the program to print: Harry is from Number Four, Privent Drive
Ron is from The Burrow
Draco is from Malfoy Manor.


And code we have written in Python
import csv

students = []

with open("harrypotter.csv") as file:
    reader = csv.reader(file)
    for name, home in reader:
        students.append({"name": name, "home": home})

for student in sorted(students, key=lambda student: student["name"]):
    print(f"{student['name']} is from {student['home']}")
When lecturer in the video runs this line of code he gets the result we are looking for, But when I run it I get following Error Message:
Error:
line 7, in <module> for name, home in reader: ^^^^^^^^^^ ValueError: too many values to unpack (expected 2)
And I am not sure why. I've looked and read the code multiple times, I've looked at what the lecturer wrote and compared and I just don't understand Why mines is failing and yet when he ran his it passed and worked.

Any ideas?


The link to the video I am watching is
https://www.youtube.com/watch?v=nLRL_NcnK-4&t=29112s

Time Stamps:
Example starts at 8:00:05
Running the code I wrote above is at 8:04:30
deanhystad write Jul-21-2023, 04:14 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply
#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
#4
One problem is that the "," in "Number Four, Privent Drive" is interpreted as a delimiter, so Harry has 3 values: "Harry", "Number Four" and "Privent Drive", but the other students only have two; name and address.

A second problem is ' The Burrow', ' Malfoy Manor' and ' "Number Four'. all start with a blank. You don't want those extra spaces at the start of the address.

I suggest you read about the csv.reader to understand all the optional arguments. I bet there is a flag that will fix at least one of the problems above.
Reply
#5
Tricky using the delimiter within a data field! I use Linux, not sure how things work for those poor Windows-users!

Starting with this data in the csv:

Quote:Name, Info
Harry, Number Four\, Privent Drive
Ron, The Burrow
Draco, Malfoy Manor
Snake, Python\, The Crusher
Hermione, "The Bitch, bigtime"

The following gives 3 fields for the last entry:

import csv
path2csv = '/home/pedro/myPython/csv/csv/harry_potter.csv'

# this will give 3 fields for the last row
with open(path2csv) as csv_file:
    # have to escape the escape character so write a double escape character \\
    csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"', escapechar='\\')
    for row in csv_reader:
        print(row)
        print('This row has', len(row), 'fields')
Output:
['Name', ' Info'] This row has 2 fields ['Harry', ' Number Four, Privent Drive'] This row has 2 fields ['Ron', ' The Burrow'] This row has 2 fields ['Draco', ' Malfoy Manor'] This row has 2 fields ['Snake', ' Python, The Crusher'] This row has 2 fields ['Hermione', ' "The Bitch', ' bigtime"'] This row has 3 fields
But this gives 2 fields for all rows.

# this correctly give 2 fields for each row
# even when that bitch Hermione has a delimiter character in the second field
with open(path2csv) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"', escapechar='\\')
    dialect = csv.Sniffer().sniff(csv_file.read(1024))
    csv_file.seek(0)
    csv_reader = csv.reader(csv_file, dialect)
    for row in csv_reader:
        print(row)
        print('This row has', len(row), 'fields')
Output:
['Name', 'Info'] This row has 2 fields ['Harry', 'Number Four\\', 'Privent Drive'] This row has 3 fields ['Ron', 'The Burrow'] This row has 2 fields ['Draco', 'Malfoy Manor'] This row has 2 fields ['Snake', 'Python\\', 'The Crusher'] This row has 3 fields ['Hermione', 'The Bitch, bigtime'] This row has 2 fields
I read Unix type default is to quote all fields when creating the csv. But my data above is not all with double quotes.

A link to the docs for csv.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Any help is appreciated! Butch12 3 735 Jul-22-2023, 03:23 AM
Last Post: Larz60+
  code not working when executed from flask app ThomasDC 1 902 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 1,761 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,019 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,088 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,942 Mar-21-2022, 10:12 AM
Last Post: End3r
  interloper help appreciated combining two python3 scripts to add new conditions. Ricktoddfrank 0 1,475 Jun-22-2021, 10:24 PM
Last Post: Ricktoddfrank
  I don't undestand why my code isn't working. RuyCab 2 1,998 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,919 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,707 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