Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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; 'column': 0.07; 'finally:': 0.07; 'subject:adding': 0.07; 'subject:getting': 0.07; 'subject:help': 0.08; 'etc).': 0.09; 'f.close()': 0.09; 'filename': 0.09; 'try:': 0.09; '-tkc': 0.16; 'csv': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'subject: \n ': 0.16; 'subject:beginner': 0.16; 'subject:values': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'module': 0.19; 'written': 0.21; 'error': 0.23; 'expanded': 0.24; 'looks': 0.24; 'handling': 0.26; 'skip:v 30': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'thus': 0.29; 'specified': 0.30; 'extract': 0.31; 'allows': 0.31; 'file': 0.32; 'open': 0.33; 'brian': 0.33; 'subject:the': 0.34; 'subject:from': 0.34; 'something': 0.35; 'version': 0.36; 'charset :us-ascii': 0.36; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'more': 0.64; 'subject:. ': 0.67; 'received:50.22': 0.84 Date: Wed, 11 Dec 2013 13:20:27 -0600 From: Tim Chase To: python-list@python.org Subject: Re: adding values from a csv column and getting the mean. beginner help In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386789555 news.xs4all.nl 2840 [2001:888:2000:d::a6]:50808 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61607 On 2013-12-11 11:10, brian cleere wrote: > filename = sys.argv[1] > column = int(sys.argv[2]) > > for line in filename() , column (): > elements = line.strip().split(',') > values.append(int(elements[col])) 1) you need to open the file 2) you need to make use of the csv module on that file 3) you need to extract the column Thus it would looks something like column = int(sys.argv[2]) f = open(sys.argv[1], "rb") r = csv.reader(f) try: for row in r: values.append(int(row[column])) finally: f.close() which can be obtusely written as values = [int(row[column]) for row in csv.reader(open(sys.argv[1], "rb"))] though the more expanded version allows you to do better error handling (rows with insufficient columns, non-numeric/non-integer values in the specified column, etc). -tkc