Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'column': 0.07; 'subject:adding': 0.07; 'subject:getting': 0.07; 'sys': 0.07; 'subject:help': 0.08; 'explanation': 0.09; 'filename': 0.09; 'fix.': 0.09; 'formatting': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'language.': 0.14; '%d:': 0.16; 'col': 0.16; 'csv': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:[ 30': 0.16; 'subject: \n ': 0.16; 'subject:beginner': 0.16; 'subject:values': 0.16; 'sys.exit(1)': 0.16; 'elements': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'specify': 0.24; 'stick': 0.24; 'together.': 0.24; 'skip:v 30': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'appreciated.': 0.29; 'that.': 0.31; 'file': 0.32; 'open': 0.33; 'brian': 0.33; 'style': 0.33; 'subject:the': 0.34; 'subject:from': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'module.': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'above,': 0.60; 'new': 0.61; "you're": 0.61; 'you.': 0.62; "you've": 0.63; 'our': 0.64; 'number:': 0.66; 'subject:. ': 0.67; 'received:2': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: adding values from a csv column and getting the mean. beginner help Date: Wed, 11 Dec 2013 19:34:30 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-2-98-195-123.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386790461 news.xs4all.nl 2881 [2001:888:2000:d::a6]:60192 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61610 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