The sum of numbers in a line from a file


K

kxjakkk

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()])
 
Ad

Advertisements

J

John Gordon

In said:
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()])

This should get you started. However, this code does not work for all
your imput lines, as 'name3' is missing the third field. You'll have to
to modify the code to do something smarter than simple space-delimited
data. But as I said, it's a start.

# open the file
with open('file') as fp:

# process each line
for line in fp.readlines():

# split the line into a list of fields, delimited by spaces
fields = line.split()

# grab the name
name = fields[0]

# convert text values to integers and sum them
sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])
sum2 = int(fields[7]) + int(fields[8])

# compute the averages
average1 = sum1 / 3.0
average2 = sum2 / 2.0

# display output
print '%s %f %f' % (name, average1, average2)
 
D

Dave Angel

kxjakkk said:
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.
 
K

kjakupak

What I've got is
def stu_scores():
lines = []
with open("file.txt") as f:
lines.extend(f.readlines())
return ("".join(lines[11:]))

scores = stu_scores()
for line in scores:
fields = line.split()
name = fields[0]
sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])
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.
 
J

Joel Goldstick

What I've got is
def stu_scores():
lines = []
with open("file.txt") as f:
lines.extend(f.readlines())
return ("".join(lines[11:]))

scores = stu_scores()
for line in scores:
fields = line.split()
name = fields[0]
Print fields here to see what's up
sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])
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.
 
K

kjakupak

scores = stu_scores()
for line in scores:
fields = line.split()
name = fields[0]
print (fields)

Error comes up saying "IndexError: list index out of range."
 
Ad

Advertisements

C

Chris “Kwpolska†Warrick

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.
 
P

Peter Otten

Error comes up saying "IndexError: list index out of range."

OK, then the 12th line starts with a whitespace char. Add more print
statements to see the problem:
scores = stu_scores()

print scores
for line in scores:

print repr(line)
fields = line.split()
name = fields[0]
print (fields)
Hint:

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

Why did you "".join the lines?
 
M

MRAB

What I've got is
def stu_scores():
lines = []
with open("file.txt") as f:
lines.extend(f.readlines())
return ("".join(lines[11:]))

scores = stu_scores()
for line in scores:
fields = line.split()
name = fields[0]
sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])
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.
Apart from the other replies, the final print is wrong. It should be:

print "%s %f %f" % (name, average1, average2)
 
T

Travis Griggs

kxjakkk said:
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
 
J

Johannes Schneider

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)






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
(e-mail address removed)
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
 
Ad

Advertisements

S

sffjunkie

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.

The following solution works for Python3 (due to the unpacking using the * syntax)

----

from collections import defaultdict, namedtuple

info = namedtuple('info', 'sum avg')

interesting_data = (x.strip(' \n') for idx, x in enumerate(open('file').readlines()) if idx > 1 and len(x.strip(' \n')) > 0)

split_points = [2, 4, 5]

results = defaultdict(list)
for line in interesting_data:
name, _, _, _, *rest = line.split()

last_point = 0
for point in split_points:
s = sum(map(int, rest[last_point:point]))
i = info(s, s / (point - last_point))

results[name].append(i)
last_point = point

print(results)
print(results['name3'][0].avg)

--Simon Kennedy
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top