嗨,我想创建一个读取文本文件并打印文件中每个人的平均值的代码,我将其保存在文件夹中
boys.txt
ed lin,3,1,4,2
thomas block,6,3,3
Functions.py
def read_txt(file_txt):
file = open(file_txt, "r")
lines = file.readlines()
file.close()
return lines
from Functions import read_txt
file_txt = input("input file: ")
read_txt(file_txt)
lines = (sum(lines)/len(lines))
print(lines)
所以我想打印像
ed lin: 2.5
thomas block: 4
但是我不知道如何继续,用我的代码我得到了错误
尝试将其他文件更改为:
from Functions import read_txt
file_txt = input("input file: ")
lines read_txt(file_txt)
lines = '\n'.join(['%s: %s' % (line.split(',')[0], sum(list(map(int, line.strip().split(',')[1:]))) / len(line.strip().split(',')[1:])) for line in lines])
print(lines)
输出:
ed lin: 2.5
thomas block: 4
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句