我開始使用Python,並在這裏有以下問題。For循環在Python中讀取多個.csv文件
縮進錯誤與計數+ = 1和其它幾行
不掃描目錄中的所有的.csv文件。當我運行這個腳本時,它只顯示第一列中只有一個.csv文件而不是多個.csv文件輸出的輸出。我的循環命令必須有一個問題。
我需要取文件每一行的標準偏差,並取每個文件中所有行的標準偏差的平均值。
#!/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)
我做了這個改變。謝謝。你知道碰巧知道爲什麼我用計數+ = 1來計算標準偏差的平均值時出現錯誤嗎? – 2012-08-08 23:09:35