OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

ValueError using csv.reader() in PyCharm

  • Thread starter Thread starter norn
  • Start date Start date
N

norn

Guest
I'm going through Harvards CS50 Python course, in todays lesson we were introduced to the csv library. I think my code and csv text file contain the exact same text as my instructor but I'm getting a ValueError when using csv.reader(), I'm assuming because of the extra comma in the first key value pair. I'm under the impression that this is exactly what the .reader() function is supposed to prevent? My PyCharm is running the most recent version of python available.

My csv file when opened in the text editor reads as follows:

Harry,Number Four, Privet Drive

Ron,The Burrow

Draco,Malfoy Manor

My code is as follows:

Code:
import csv

students = []
with open("students.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"]}')

I've obviously tried changing the first "home" value to a string, so I understand it can be fixed that way. I'm more-so confused why my csv.reader(file) isn't ignoring the second comma, aka its intended purpose.

The entire error message returns:

python csvtest.py Traceback (most recent call last): File "/Users/Julie/PycharmProjects/pythonProject/csvtest.py", line 10, in for name,home in reader: ^^^^^^^^^ ValueError: too many values to unpack (expected 2)
<p>I'm going through Harvards CS50 Python course, in todays lesson we were introduced to the csv library. I think my code and csv text file contain the exact same text as my instructor but I'm getting a ValueError when using csv.reader(), I'm assuming because of the extra comma in the first key value pair. I'm under the impression that this is exactly what the .reader() function is supposed to prevent? My PyCharm is running the most recent version of python available.</p>
<p>My csv file when opened in the text editor reads as follows:</p>
<p>Harry,Number Four, Privet Drive</p>
<p>Ron,The Burrow</p>
<p>Draco,Malfoy Manor</p>
<p>My code is as follows:</p>
<pre><code>import csv

students = []
with open("students.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"]}')
</code></pre>
<p>I've obviously tried changing the first "home" value to a string, so I understand it can be fixed that way. I'm more-so confused why my csv.reader(file) isn't ignoring the second comma, aka its intended purpose.</p>
<p>The entire error message returns:</p>
<p>python csvtest.py
Traceback (most recent call last):
File "/Users/Julie/PycharmProjects/pythonProject/csvtest.py", line 10, in
for name,home in reader:
^^^^^^^^^
ValueError: too many values to unpack (expected 2)</p>
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top