Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61610
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: adding values from a csv column and getting the mean. beginner help |
| Date | 2013-12-11 19:34 +0000 |
| References | <e3a75021-32a2-471e-b486-51d1b28ffef6@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3924.1386790461.18130.python-list@python.org> (permalink) |
On 11/12/2013 19:10, brian cleere wrote:
> I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated.
>
> #!/bin/env python
> import csv
You never use the csv module.
> import sys
>
> if len(sys.argv) < 3:
> print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0]))
> sys.exit(1)
>
> filename = sys.argv[1]
> column = int(sys.argv[2])
>
> for line in filename() , column ():
You're trying to loop around the filename and the column, you need to
open the file and loop around that.
> elements = line.strip().split(',')
Please don't do this when you've got the csv module to do things for you.
> values.append(int(elements[col]))
Where did values come from? Is it col or column, please make your mind up?
So let's stick things together. Something like.
values = []
with open(filename) as csvfile:
valuereader = csv.reader(csvfile)
for row in valuereader:
values.append(int(row[column]))
>
> csum = sum(values)
> cavg = sum(values)/len(values)
> print("Sum of column %d: %f" % (col, csum))
> print("Avg of column %d: %f" % (col, cavg))
I like consistency, new style formatting here, old style above, still if
it works for you.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
adding values from a csv column and getting the mean. beginner help brian cleere <briancleere@gmail.com> - 2013-12-11 11:10 -0800 Re: adding values from a csv column and getting the mean. beginner help Tim Chase <python.list@tim.thechases.com> - 2013-12-11 13:20 -0600 Re: adding values from a csv column and getting the mean. beginner help Chris Angelico <rosuav@gmail.com> - 2013-12-12 06:22 +1100 Re: adding values from a csv column and getting the mean. beginner help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-11 19:34 +0000 Re: adding values from a csv column and getting the mean. beginner help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-11 19:41 +0000 Re: adding values from a csv column and getting the mean. beginner help Chris Angelico <rosuav@gmail.com> - 2013-12-12 06:46 +1100 Re: adding values from a csv column and getting the mean. beginner help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-11 20:00 +0000 Re: adding values from a csv column and getting the mean. beginner help Chris Angelico <rosuav@gmail.com> - 2013-12-12 07:03 +1100 Re: adding values from a csv column and getting the mean. beginner help Tim Chase <python.list@tim.thechases.com> - 2013-12-11 14:20 -0600 Re: adding values from a csv column and getting the mean. beginner help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-11 20:20 +0000 Re: adding values from a csv column and getting the mean. beginner help Christopher Welborn <cjwelborn@live.com> - 2013-12-11 18:49 -0600 Re: adding values from a csv column and getting the mean. beginner help "M.F." <morefool@gmail.com> - 2013-12-12 09:36 +0800
csiph-web