Python初心者。 ファイルのデータを表示し、各人の合計/平均を計算するにはどうすればよいですか? どのように値をの外にある変数にforを追加するのですか?繰り返しがレコードの数で分割されると、その値は分割されます。ファイルからの平均と合計を計算する
ファイル内のデータは、ユーザーがデータを追加したり、削除したりすることができますので、変化するが、データは次のように構成されています
PersonA 342 454 559 TOTAL AVERAGE
PersonB 444 100 545 TOTAL AVERAGE
PersonC 332 567 491 TOTAL AVERAGE
PersonD 142 612 666 TOTAL AVERAGE
何をすることができます:私はこのようにそれを提示したい
PersonA;342;454;559;
PersonB;444;100;545;
PersonC;332;567;491;
PersonD;142;612;666;
私はそれを正しくするためにこの後に書き込みますか?
def show_result():
text_file = open('result.txt', 'r')
for line in text_file:
if ';' in line:
line2 = line.split(";")
print line2
?
with open("input.txt") as f:
for line in f:
s = """PersonA;342;454;559;
PersonB;444;100;545;
PersonC;332;567;491;
PersonD;142;612;666;"""
for line in s.split("\n"):
p, a, b, c, _ = line.strip().split(";")
print("{}\t{}\t{}\t{}\t{}\t{}".format(p, a, b, c,
sum([int(a), int(b), int(c)]),
sum([int(a), int(b), int(c)])/3))
ソリューション:
def show_result():
text_file = open('minigolf.txt', 'r')
print "Name,Round1,Round2,Round3"
for line in text_file:
if ';' in line:
line2 = line.split(";")[:-1]
print line2
line_total = sum(map(int, line2[1:]))
line_average = line_total/len(line2[1:])
print "Total: ", line_total
print "Average: ", line_average
合計と平均一人一人のために計算されている方法は?列2,3,4の合計/平均を意味しますか? –