파일 입출력 연습문제
student.csv에는 아래와 같은 텍스트가 담겨있습니다.
학년,반,번,이름,국어,영어,수학,사회
3,3,1,licat,90,80,30,40
3,3,2,mura,80,70,60,30
3,3,3,binky,30,80,70,30학년,반,번,이름,국어,영어,수학,사회
3,3,1,licat,90,80,30,40
3,3,2,mura,80,70,60,30
3,3,3,binky,30,80,70,30학생들의 평균을 구해 아래와 같이 student.csv출력되게 해주세요. xx이라 표기된 곳에 평균 값이 들어가야 합니다.
학년,반,번,이름,국어,영어,수학,사회,평균
3,3,1,licat,90,80,30,40,xx
3,3,2,mura,80,70,60,30,xx
3,3,3,binky,30,80,70,30,xx학년,반,번,이름,국어,영어,수학,사회,평균
3,3,1,licat,90,80,30,40,xx
3,3,2,mura,80,70,60,30,xx
3,3,3,binky,30,80,70,30,xx정답 코드
s = ''
with open('student.csv', 'r') as f:
lines = f.readlines()
s += lines[0][:-1] + ',평균'
for i in lines[1:]:
data = i.split(',')
s += f'\n{i.strip()},{sum(map(int, data[4:])) / 4}'
with open('student.csv', 'w') as f:
f.write(s)s = ''
with open('student.csv', 'r') as f:
lines = f.readlines()
s += lines[0][:-1] + ',평균'
for i in lines[1:]:
data = i.split(',')
s += f'\n{i.strip()},{sum(map(int, data[4:])) / 4}'
with open('student.csv', 'w') as f:
f.write(s)