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


Groups > comp.lang.python > #61607

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

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: adding values from a csv column and getting the mean. beginner help
In-Reply-To <e3a75021-32a2-471e-b486-51d1b28ffef6@googlegroups.com>
References <e3a75021-32a2-471e-b486-51d1b28ffef6@googlegroups.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3921.1386789554.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next 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