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


Groups > comp.lang.python > #66811

Re: The sum of numbers in a line from a file

Date 2014-02-21 09:16 +0100
From Johannes Schneider <johannes.schneider@galileo-press.de>
Subject Re: The sum of numbers in a line from a file
References <882091da-a499-477e-8f50-c5bdde7cdfec@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.7218.1392970584.18130.python-list@python.org> (permalink)

Show all headers | View raw


s = 4
e = 7
with f = file('path_to_file) as fp:
	for line in f:
		if line.startswith(name):
			avg =  sum(map(int, filter(ambda x : len(x) > 0,
				s.split(' '))[s : e])) / (e -  s)






On 20.02.2014 17:22, kxjakkk wrote:
> Let's say I have a sample file like this:
>
> Name        1           2     3    4     5      6     7    8
> ------------------------------------------------------------------------
> name1    099-66-7871   A-F    Y    100    67    81    59    98
> name2    999-88-7766   A-F    N    99   100    96    91    90
> name3    000-00-0110    AUD        5    100    28    19    76
> name4    398-72-3333    P/F    Y    76    84    49    69    78
> name5    909-37-3689    A-F    Y    97    94    100    61    79
>
> For name1, I want to add together columns 4, 5, 6, and get an average from that, then do the same for the last two columns. I want to do this for every name.
>
> All I've got is
> sum([int(s.strip()) for s in open('file').readlines()])
>


-- 
Johannes Schneider
Webentwicklung
johannes.schneider@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

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


Thread

The sum of numbers in a line from a file kxjakkk <kjakupak@gmail.com> - 2014-02-20 08:22 -0800
  Re: The sum of numbers in a line from a file John Gordon <gordon@panix.com> - 2014-02-20 16:46 +0000
    Re: The sum of numbers in a line from a file kjakupak@gmail.com - 2014-02-20 10:16 -0800
      Re: The sum of numbers in a line from a file Joel Goldstick <joel.goldstick@gmail.com> - 2014-02-20 13:32 -0500
        Re: The sum of numbers in a line from a file kjakupak@gmail.com - 2014-02-20 10:37 -0800
          Re: The sum of numbers in a line from a file Peter Otten <__peter__@web.de> - 2014-02-20 19:59 +0100
      Re: The sum of numbers in a line from a file Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2014-02-20 19:56 +0100
      Re: The sum of numbers in a line from a file MRAB <python@mrabarnett.plus.com> - 2014-02-20 19:17 +0000
  Re:The sum of numbers in a line from a file Dave Angel <davea@davea.name> - 2014-02-20 11:54 -0500
  Re: The sum of numbers in a line from a file Travis Griggs <travisgriggs@gmail.com> - 2014-02-20 14:35 -0800
  Re: The sum of numbers in a line from a file Johannes Schneider <johannes.schneider@galileo-press.de> - 2014-02-21 09:16 +0100
  Re: The sum of numbers in a line from a file sffjunkie@gmail.com - 2014-02-24 03:48 -0800
    Re: The sum of numbers in a line from a file sffjunkie@gmail.com - 2014-02-24 04:02 -0800

csiph-web