adding values from a csv column and getting the mean. beginner help

Chris Angelico rosuav at
Wed Dec 11 20:22:09 CET 2013

On Thu, Dec 12, 2013 at 6:10 AM, brian cleere <briancleere at> wrote:
> I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated.

Your problem is akin to debugging an empty file :) It's not so much a
matter of fixing what's not working as of starting at the very
beginning: How do you iterate over the content of a CSV file?

Now, you're almost there... partly. You have the split() call, which
will split on the comma, so if you go that route, all you need to do
is open the file, using the aptly-named builtin function "open".
You'll find docs on that if you do a quick search.

But you're actually part-way to the better solution. You're importing
the 'csv' module, which is exactly what you need here. All you need is
to read up on its docs:

I'm sure you can figure out the rest of your homework from there!

Now, with that out of the way, I'd like to just mention a couple of
other things.

>    print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0]))

Square brackets in a usage description often mean "optional". You may
want to be careful of that. There's no really good solution though.

> csum = sum(values)
> cavg = sum(values)/len(values)

Once you've calculated the sum once, you can reuse that to calculate
the average. Can you see how? :)

And finally: You're using Google Groups to post, which means your
paragraphs are unwrapped, and - unless you fight very hard against a
stupidly buggy piece of software - your replies will be malformed and
ugly. Don't make yourself look bad; switch to a better newsreader, or
to the mailing list:

The content is the same, you just subscribe to the list and read and
write as email.

Thanks! And welcome to the group.


More information about the Python-list mailing list