我开始使用 Python 并在这里遇到以下问题。
count +=1 和其他几行的缩进错误
不扫描目录中的所有 .csv 文件。当我运行这个脚本时,它只显示一个 .csv 文件的输出,而不是第一列的多个 .csv 文件输出。我拥有的 for 循环命令一定有问题。
我需要取文件每一行的标准偏差,并取每个文件中所有行的标准偏差的平均值。
#!/usr/bin/env python import os print "Filename, Min, Max, Average, Mean of Std" for file in os.listdir("."): if not file.endswith(".csv"): continue csv = open(file) sum = 0 diff = 0 std = 0 sumstd = 0 count = 0 min = 0 max = 0 for line in csv.readlines(): x = line.split(",") time = x[0] value = float(x[1]) sum += value if value > max: max = value if 0 < value < min: min = value count += 1 avg = sum / count import math count +=1 diff += (value - avg)**2 std = math.sqrt (diff / (count+1)-1) sumstd += std meanstd = sumstd/count print file + "," + str(min) + "," + str(max) + "," + str(avg) + "," + str(meanstd)