Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61640

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

From "M.F." <morefool@gmail.com>
Newsgroups comp.lang.python
Subject Re: adding values from a csv column and getting the mean. beginner help
Date 2013-12-12 09:36 +0800
Organization Aioe.org NNTP Server
Message-ID <l8b3u8$lru$1@speranza.aioe.org> (permalink)
References <e3a75021-32a2-471e-b486-51d1b28ffef6@googlegroups.com>

Show all headers | View raw


On 12/12/2013 03:10 AM, 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
> 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 ():
>      elements = line.strip().split(',')
>      values.append(int(elements[col]))

"filename" is a string, and "column" is an integer, the interpreter 
should warn you that they are not callable
the above three lines could be changed to
==>
values = []
for line in open(filename):
      elements = line.strip().split(',')
      values.append(int(elements[column]))

or now that you have the "csv" module, you can use "csv.reader" to parse 
the file:
==>
values = [ row[column] for row in csv.reader(open(filename, 'rb')) if 
len(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))
>

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


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