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

2013-12-11 Thread M.F.

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))



--
https://mail.python.org/mailman/listinfo/python-list


Re: How to add a current string into an already existing list

2013-11-05 Thread M.F.

On 11/05/2013 04:54 PM, Nick the Gr33k wrote:



===
data = cur.fetchall

data = cur.fetchall()
That is what the stack trace and Christ tried to inform you.

for row in data:
===

The only thing i can understand by looking the above 2 lines is this:

'for' fails to iterate over 'data' because for some reason 'data'
haven't resulted as a list of rows that can be iterated row by row.

But that just doesn't help me much.


--
https://mail.python.org/mailman/listinfo/python-list