- 帖子
- 2
- 精华
- 0
- 积分
- 7
- 阅读权限
- 10
- 注册时间
- 2018-7-31
- 最后登录
- 2018-7-31
|
f = open('score.txt',errors='ignore')
lines = f.readlines()#readlines,把每一行分开
print (lines)
f.close()
results = []
for line in lines:
print (line)
data = line.split()
print (data)
sum = 0
for score in data[1:]:
sum += int(score)
result =( '%s \t: %d\n' % (data[0], sum))
print (result)
results.append(result)
print (results)
output = file('result.txt', 'w')
output.writelines(results)
output.close()
运行时会出现如下错误:
Traceback (most recent call last):
File "C:/Users/ting/day7.py", line 80, in <module>
result =( '%s \t: %d\n' % (data[0], sum))
IndexError: list index out of range
|
|