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


Groups > comp.lang.python > #66781

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <kwpolska@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'subject:file': 0.07; 'utf-8': 0.07; 'item,': 0.09; 'moreover,': 0.09; '\xe2\x80\x94': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'character.': 0.16; 'iterates': 0.16; 'itself,': 0.16; 'line.split()': 0.16; 'splitting': 0.16; 'whitespace.': 0.16; 'index': 0.16; 'wrote:': 0.18; '3.0': 0.19; 'thu,': 0.19; 'starts': 0.20; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'replace': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; '2.0': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'character': 0.29; 'thus': 0.29; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'url:mailman': 0.30; 'lines': 0.31; 'letter.': 0.31; 'subject:numbers': 0.31; 'says': 0.33; 'url:python': 0.33; 'table': 0.34; 'sense': 0.34; 'subject:from': 0.34; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'operating': 0.37; 'turn': 0.37; 'list': 0.37; 'list.': 0.37; 'pm,': 0.38; 'url:mail': 0.40; 'range': 0.61; 'from:charset:utf-8': 0.61; 'name': 0.63; 'subject:The': 0.64; 'skip:\xe2 10': 0.65; 'to:addr:gmail.com': 0.65; '20,': 0.68; 'containing': 0.69; '11.': 0.74; '8bit%:43': 0.74; 'it\xe2\x80\x99s': 0.84; 'scores': 0.91; 'url:tk': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=2A3KXvpw3/YV7flDHMuVS0x6QWPQbQQoh0TG8pQecL4=; b=InO/eBuCmFHeeduRl2rVNIL+y+Cgil1XJZ6IuRvqU3NsWeLWSbcGw3UWWF5yoxp6E/ Jj8Erle1QUtMKah6+3QZpGvs9Z5ABaJRDTGbG1cM9tU+1rr+2LzGMLH9dAKWY92xUk6l GRQFJ74vPJacoGlsw2je/Yn83PwjB6JJ/Ip3nPF7So5//tZ5uXOiwecrtYL3/r7eASvP UBF5E6u/Pq1QcGd+/XV9VnzIcsuFHO6UygW6vpkCoCXZdNA48m3WebsyyKclTpCeohxw hJyQcNVMVhtHvfYMtzyeKy+tImImBluw1ofdJ/tulWOlW4fwcmE3mHyiCmYcD0GLGVxK jVow==
MIME-Version 1.0
X-Received by 10.50.138.104 with SMTP id qp8mr8170228igb.39.1392922607538; Thu, 20 Feb 2014 10:56:47 -0800 (PST)
In-Reply-To <9036049f-7d08-4de7-83f8-61e5fa2c2d2f@googlegroups.com>
References <882091da-a499-477e-8f50-c5bdde7cdfec@googlegroups.com> <le5bhe$461$1@reader1.panix.com> <9036049f-7d08-4de7-83f8-61e5fa2c2d2f@googlegroups.com>
Date Thu, 20 Feb 2014 19:56:47 +0100
Subject Re: The sum of numbers in a line from a file
From Chris “Kwpolska” Warrick <kwpolska@gmail.com>
To kjakupak@gmail.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.7202.1392922619.18130.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392922619 news.xs4all.nl 2856 [2001:888:2000:d::a6]:42046
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66781

Show key headers only | View raw


On Thu, Feb 20, 2014 at 7:16 PM,  <kjakupak@gmail.com> wrote:
> What I've got is
> def stu_scores():
>     lines = []
>     with open("file.txt") as f:
>         lines.extend(f.readlines())
>     return ("".join(lines[11:]))

This returns a string, not a list.  Moreover, lines.extend() is
useless. Replace this with:

def stu_scores():
    with open("file.txt") as f:
        lines = f.readlines()
    return lines[11:]

> scores = stu_scores()
> for line in scores:

`for` operating on strings iterates over each character.

>     fields = line.split()

Splitting one character will turn it into a list containing itself, or
nothing if it was whitespace.

>     name = fields[0]

This is not what you want it to be — it’s only a single letter.

>     sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])

Thus it fails here, because ['n'] has just one item, and not nine.

>     sum2 = int(fields[7]) + int(fields[8])
>     average1 = sum1 / 3.0
>     average2 = sum2 / 2.0
>     print ("%s %f %f %") (name, average1, average2)
>
> It says that the list index is out of range on the sum1 line. I need stu_scores because the table from above starts on line 11.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense

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