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

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Dec 11 14:41:06 EST 2013


On 11/12/2013 19:22, Chris Angelico wrote:
> On Thu, Dec 12, 2013 at 6:10 AM, brian cleere <briancleere at gmail.com> 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:
>
> http://docs.python.org/3/library/csv.html
>
> 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.

There is, https://pypi.python.org/pypi/docopt/0.6.1 :)

>
>> 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:
>
> https://mail.python.org/mailman/listinfo/python-list
>
> The content is the same, you just subscribe to the list and read and
> write as email.

Ooh 'eck, we'll have the Popular Front for the Liberation of Google 
Groups squad out in force again, vainly trying to defend the bug ridden 
crap that they insist on using, and which I obviously won't mention. 
Whoops!!!

Oh Lord, won't you buy me Mozilla Thunderbird ?
My friends all use GG, I think that's absurd.
Worked hard all my lifetime, no help from the nerds,
So Lord, won't you buy me Mozilla Thunderbird ?

With apologies to the late, great Janis Joplin.

>
> Thanks! And welcome to the group.
>
> ChrisA
>

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list