0

I searched through the python csv questions, can't seem to find anything addressing the reader error I'm getting.

I have a csv file with 6 columns, trying to access two of them:

import csv

with open('/home/conjugater/Downloads/aapl.csv', newline='') as csvfile:
  dialect=csv.Sniffer().sniff(csvfile.read(1024))
  csvfile.seek(0)
  reader=csv.reader(csvfile, dialect)
  for row in reader:
      for Date, Open, High, Low, Close, Volume in row:
          print(Date, Close)

I'm not done coding, but this is my first time using the csv module and I'm just trying to get a feel for it (relatively new to python in general).

Traceback (most recent call last):
File "reader.py", line 8, in <module>
for Date, Open, High, Low, Close, Volume in row:
ValueError: need more than 5 values to unpack

I can get the columns I want with

for row in reader:
    print(row[0], row[4])

but I know there's a way to assign each column a name with python as well; any help much appreciated.

2
  • looks like your file has only 5 columns. Commented Jun 12, 2014 at 17:54
  • @dano print(len(row)) returns 6 for every row Commented Jun 12, 2014 at 17:57

3 Answers 3

3

You have 2 for loops when you only need one:

for (Date, Open, High, Low, Close, Volume) in reader:
    print(Date, Close)
Sign up to request clarification or add additional context in comments.

5 Comments

I think he probably wants for (Date, Open, High, Low, Close, Volume) in reader:. But yeah, that's his problem.
true, kinda new to python (and programming in general), not yet as efficient as I'd like to be. Thanks for pointing that out, I'm trying to develop efficient coding practices
@bushc Not sure if you realized this or not, but this is the actual cause of your error. Doing for Date, Open, High, Low, Close, Volume in row: like you are is actually iterating over each of the six elements in the row tuple one at a time.
@dano does that slow things down? not sure I understand why that's a bad thing?
@bushc It means that right now, you're doing this: for Date, Open, High, Low, Close, Volume in ("6/12/15", "20.00", "21.00", "19.00", "20.58", "20"): Which is not what you're actually trying to do. That will iterate through the tuple, taking each element, and trying to unpack it to Date, Open, High, Low, Close, Volume. So first it will try to unpack "6/12/2014", which will of course fail, and cause the ValueError you're seeing.
1

Why are you assigning newline=''? That tells open() to treat each empty string as the end of a line. Try this instead: newline='\n'. Or try it without passing newline.

Vor's comment below is useful too. See docs on open for more information.

1 Comment

@Vor docs.python.org/3/library/csv.html says to use newline='' for file objects
1

You're close, but you need to use

with open('/home/conjugater/Downloads/aapl.csv', newline='') as csvfile:
  dialect=csv.Sniffer().sniff(csvfile.read(1024))
  csvfile.seek(0)
  reader=csv.DictReader(csvfile, dialect, fieldnames = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])
  for row in reader:
      for Date, Open, High, Low, Close, Volume in row:
          print("{}: {}".format(Date, Close))

Hope that helps....

1 Comment

thanks, I'll check that out after I try @dano 's fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.