Hello, i would like to make program in python which reads the lines from an input file, which looks like this:

Bob
10
20
30
John
15
15
Kate
20

and the program sums the numbers for each person and writes into an output file which should look like this:

Bob
60
John
30
Kate
20

So far i can read the input file and write everything into the output file.

input = file("input.txt", "r")
output = file("output.txt", "w")   
line = input.readline()
while line:
    output.write(line)
    line = input.readline()
input.close()                    
output.close()

I tried to sum the numbers but it says error because of the alphabetic. I wanted to sum the lines like line2 + line 3 + line4, line 6 + line 7, line 9 but i don't know how to sum lines.

This should work..

import re
pattern = '(\D+)\n'
pattern2 = '(\d+)'
s = re.compile(pattern2)
p = re.compile(pattern)
input = file("test.txt", "r")
output = file("output.txt", "w")
sum = 0
name = None; old = None
while True:
    line = input.readline()    
    out = p.findall(line)
    num = s.findall(line)    
    if line is None or line == "":
        output.write(name)
        output.write(str(sum)+'\n')
        break
    if out:        
        if name == old and name != None: output.write(name)
        name= line;
        if sum != 0:output.write(str(sum)+'\n');sum = 0;
    elif num: sum = sum+ int(num[0]); old = name    
output.close()
input.close()

Hello, i would like to make program in python which reads the lines from an input file, which looks like this:

Bob
10
20
30
John
15
15
Kate
20

and the program sums the numbers for each person and writes into an output file which should look like this:

Bob
60
John
30
Kate
20

So far i can read the input file and write everything into the output file.

input = file("input.txt", "r")
output = file("output.txt", "w")   
line = input.readline()
while line:
    output.write(line)
    line = input.readline()
input.close()                    
output.close()

I tried to sum the numbers but it says error because of the alphabetic. I wanted to sum the lines like line2 + line 3 + line4, line 6 + line 7, line 9 but i don't know how to sum lines.

Very interesting homework problem.

Note about your code:
input is Python function name and should not be used for variable name, also use open() rather then file(). Function file() has been removed in Python3.

Thanks, now i would like to extend it with argument check, so if i write in console/putty:

python test.py
it should write: not enough arguments

python test.py something
it should write: this file doesn't exists

python test.py test.txt
it should work

i tried to put this into the code but it says error for except:

import errno

...

infile = open("test.txt", "r")

except IOError, e:
  if e.errno == errno.ENOENT:
    print "file doesn't exist"
print infile

outfile = open("output.txt", "w")

...

És ha a tanár belekérdez eltudod majd magyarázni? - Nekem nem lenne pofám így beadni....

Thanks, now i would like to extend it with argument check, so if i write in console/putty:

python test.py
it should write: not enough arguments

python test.py something
it should write: this file doesn't exists

python test.py test.txt
it should work

i tried to put this into the code but it says error for except:

import errno

...

infile = open("test.txt", "r")

except IOError, e:
  if e.errno == errno.ENOENT:
    print "file doesn't exist"
print infile

outfile = open("output.txt", "w")

...

You might want to read some Exception handling in python. There is a lot of stuff present online.
You cannot use "except" without a "try" block:

import errno
try:
    infile = open("test.txt", "r")
 
except IOError, e:
  if e.errno == errno.ENOENT:
    print "file doesn't exist"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.