The sum of numbers in a line from a file
Travis Griggs
travisgriggs at gmail.com
Thu Feb 20 17:35:17 EST 2014
On Feb 20, 2014, at 8:54 AM, Dave Angel <davea at davea.name> wrote:
> kxjakkk <kjakupak at gmail.com> Wrote in message:
>> 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()])
>>
>
> Don'ttrytodoitallinoneline.thatwayyouactuallymighthaveaplacetoinse
> rtsomeextralogic.
>
Yes.
Clearly
the
preferred
way
to
do
it
is
with
lots
of
lines
with
room
for
expandability.
Sorry Dave, couldn’t resist. Clearly a balance between extremes is desirable.
(Mark, I intentionally put the blank lines in this time <grin>)
Travis Griggs
"“Every institution tends to perish by an excess of its own basic principle.” — Lord Acton
More information about the Python-list
mailing list