好久没写过python了天 一点点查的
import os
import re
from decimal import *
import math
'''
def getfiles():
filenames=os.listdir(r'F:filePythonvolume')
print(len(filenames))
getfiles()
'''
name = "F:\filePython\volume" #!!\
packagenames = os.listdir(r'F:filePythonvolume') #返回值是list类型
print(len(packagenames)) #得到六个子文件夹
for package in packagenames:
#print(package)
file2_path = package + ".txt"
f2 = open(file2_path,"w") #文件创建在了本目录下
filenames = os.path.join(name,package)
#print(filenames) #得到各个文件夹的绝对路径值
for filename in os.listdir(filenames):
print(filename) #得到单个文件 只是文件名 开始处理单个文件
signalname = filename
filename = os.path.join(filenames,filename)
f = open(filename,"r",encoding = "utf-8")
countz = 0
counts = 0
numberlist = []
for line in f:
#print("line: "+line)
if line.startswith('#') | line.startswith('@'):
countz += 1
else :
counts += 1
numberlist.append(line)
#print("line: "+line)
#print(type(line)) #str
#print(countz) #第25行开始数字
#print(counts) #共497
#print(len(numberlist))
usenumberlist = []
for row in numberlist:
#对每一行进行处理 得到中间的那个数 再存储 判断类型别待会转不成
usenumber = re.split(r"[ ]+", row)[2] #正则
#print(usenumber) #str
#print(type(usenumber))
usenumberlist.append(usenumber)
# 得到第二列的数值 存储在list数组
#开始类型转换 进行精确计算decimal
sum = Decimal('0.0')
for usen in usenumberlist:
#print("usen: "+usen)
sum += Decimal(usen)
#print(sum)
#计算均值
average = Decimal('0.0')
average = sum / Decimal(counts) #接受直接拿整型做参数
print(average)
straverage = str(Decimal(average))
#print(straverage)
#计算均方差
sumvariance = Decimal('0.0')
for usen in usenumberlist:
#print("usen: "+usen)
fac1 = Decimal(usen)
sumvariance += (fac1 - average)*(fac1 - average)
#print(sumvariance) #未开方前的分母
fz = sumvariance / Decimal(counts-1) #未开方前的分子
#计算标准差
#print(fz.sqrt())
StandardDeviation = fz.sqrt()
#print(type(StandardDeviation)) #<class 'decimal.Decimal'>
#本文件的内容:要写入三个part filename straverage StandardDeviation
#print(signalname)
f2.write("%-25s %35s %30s
"%(signalname, straverage, str(Decimal(StandardDeviation)))) #左靠齐输出
#f2.write("11")
'''
#都弄好了之后
#print(package)
file2_path = package + ".txt"
f2 = open(file2_path,"w") #文件创建在了本目录下
f2.write("11")
'''
f2.close()
我再说一次我不喜欢现在的代码形式