Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.080 X-Spam-Evidence: '*H*': 0.85; '*S*': 0.01; 'amtsgericht': 0.07; 'hrb': 0.07; 'subject:file': 0.07; 'len(x)': 0.09; 'columns': 0.16; 'fp:': 0.16; 'rainer': 0.16; 'ralf': 0.16; 'wrote:': 0.18; 'gmbh': 0.22; 'header:User-Agent:1': 0.23; 'germany': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'subject:numbers': 0.31; 'file': 0.32; 'subject:from': 0.34; 'add': 0.35; 'two': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'name': 0.63; 'received:194': 0.64; 'subject:The': 0.64; 'skip:+ 10': 0.65; 'tel.:': 0.65; 'sample': 0.67; 'press': 0.70; '100': 0.79; 'avg': 0.84; 'received:192.168.57': 0.84; 'average': 0.93 Date: Fri, 21 Feb 2014 09:16:19 +0100 From: Johannes Schneider User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: The sum of numbers in a line from a file References: <882091da-a499-477e-8f50-c5bdde7cdfec@googlegroups.com> In-Reply-To: <882091da-a499-477e-8f50-c5bdde7cdfec@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392970584 news.xs4all.nl 2832 [2001:888:2000:d::a6]:49223 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66811 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