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

2014-02-24 Thread sffjunkie
On Monday, 24 February 2014 11:48:23 UTC, sffj...@gmail.com wrote: > split_points = [2, 4, 5] Change this to `split_points = [3, 5]` for your requirements --Simon Kennedy -- https://mail.python.org/mailman/listinfo/python-list

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

2014-02-24 Thread sffjunkie
On Thursday, 20 February 2014 16:22:00 UTC, kxjakkk wrote: > Let's say I have a sample file like this: > Name1 2 34 5 6 78 > > name1099-66-7871 A-FY10067815

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

2014-02-21 Thread 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) On 20.02.2014 17:22, kxjakkk wrote: Let

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

2014-02-20 Thread Travis Griggs
On Feb 20, 2014, at 8:54 AM, Dave Angel wrote: > kxjakkk Wrote in message: >> Let's say I have a sample file like this: >> >> Name1 2 34 5 6 78 >> >> name1099-66-7871 A-F

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

2014-02-20 Thread MRAB
On 2014-02-20 18:16, kjaku...@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:])) scores = stu_scores() for line in scores: fields = line.split() name = fields[0]

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

2014-02-20 Thread Peter Otten
kjaku...@gmail.com wrote: > 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.

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

2014-02-20 Thread Chris “Kwpolska” Warrick
On Thu, Feb 20, 2014 at 7:16 PM, 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:

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

2014-02-20 Thread 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." -- https://mail.python.org/mailman/listinfo/python-list

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

2014-02-20 Thread Joel Goldstick
On Feb 20, 2014 1:20 PM, wrote: > > 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] Pr

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

2014-02-20 Thread 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

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

2014-02-20 Thread John Gordon
In <882091da-a499-477e-8f50-c5bdde7cd...@googlegroups.com> kxjakkk writes: > Let's say I have a sample file like this: > Name1 2 34 5 6 78 > > name1099-66-7871 A-FY1

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

2014-02-20 Thread Joel Goldstick
On Feb 20, 2014 11:25 AM, "kxjakkk" wrote: > > Let's say I have a sample file like this: > > Name1 2 34 5 6 78 > > name1099-66-7871 A-FY10067815998 > na

The sum of numbers in a line from a file

2014-02-20 Thread kxjakkk
Let's say I have a sample file like this: Name1 2 34 5 6 78 name1099-66-7871 A-FY10067815998 name2999-88-7766 A-FN99 1009691