使用函数,我如何打印从文件中读取的PAY列表的最低,最高和平均值?
try:
text_file = open ("Pay.txt", "w")
text_file.writelines(Pay)
text_file.close()
except (IOError):
print 'Error opening/writing Pay.txt'
try:
text_file= open("Pay.txt","r")
PAY_= text_file.readlines()
text_file.close()
PAY_.sort()
我从未设置过这样的内容,有人能让我开始吗?我会提前感谢您的回复。请记住,我是新来的,我不确切知道你是怎么做的......请耐心等待。
答案 0 :(得分:2)
假设每行有一个数字:
numbers = [float(line) for line in open('Pay.txt') if line.strip()]
if numbers:
print 'min', min(numbers)
print 'max', max(numbers)
print 'avg', sum(numbers) / len(numbers)
else:
print 'file is empty or all lines are blank'