-1
def reademail():
f = open("Y11email.csv", "r")
line= "email"+ ","+"fname"+","+"sname"+"password"+"\n"
for line in f:
    email,fname,sname,password=line.split(", ")#splits each line however creates an extra space between the lines because of enter
    password=password.strip()#strip removes the extra space between the lines
    print(email,fname,sname,password)
f.close()

I am new to python so I don't understand the Error I keep getting. I hope someone can explain. If you need any more information I'll edit it in.

email,fname,sname,password=line.split(", ")#splits each line however creates an extra space between the lines because of enter

ValueError: not enough values to unpack (expected 4, got 1)

I want it print like this:

email@abc.org.uk Name Surname fakepassword

check@email.org.uk Z Y fakepassword

testing@the.program Ray Check hello

Edit: I've tried to remove the space between after the comma and tried to .split("\n") but got

email,fname,sname,password=line.split("\n")#splits each line however creates an extra space between the lines because of enter

ValueError: not enough values to unpack (expected 4, got 2)

At least I got one more value XD

Community
  • 1
  • 1
Ray
  • 1
  • 1
  • 4

2 Answers2

0

Rather than re-implementing a csv reader, use the csv module from Python's standard library.

Example copied from the documentation:

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

This shows the canonical way to read a CSV file. Note that the csv.reader yields a list of strings for each iteration, so you don't have to split it.

You can test the length of the line before splitting it:

for row in Y11reader:
    ll = len(row)
    if ll != 4:
        logging.warning('invalid row of {} items'.format(ll))
        continue  # skip to next iteration.
    email, fname, sname, password = row
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
0

The problem is your input. You need to make sure that each line in your input has all these 4 characteristics, and they are always delimited by a comma.

This error occurs when the number of values on the right side on the equation do not match the number of variables on the right side. An example would be:

The following works as the line is split into a list of 4 elements

line = "1,2,3,4"
line_list = line.split(',') #Looks like [1,2,3,4]
first, second, third, fourth = line.split(',') #Each element is assigned to each variable

But the following does not work

line = "1,2,3"
line_list = line.split(',') #Looks like [1,2,3]
first, second, third, fourth = line.split(',') 

There is no one to one mapping of elements to the variables. Hence, the interpreter will throw an error saying not enough values to unpack (expected 4, got 3). As 4 values were expected in list (as there are 4 variables on the left side of the equation, but only 3 were provided.

Viraj Rai
  • 75
  • 2