python 处理多行多列多文件的数据
#coding=utf-8
import matplotlib.pyplot as plt
filename1 = '02.txt'
filename2 = '03.txt'
filename3 = 'C05.txt'
filename4 = 'CSS.txt'
fileA = open(filename4,'w') 写入新文件
X,Y,Z,W,list1,list2,list3,list4,list5 = [],[],[],[],[],[],[],[],[]
with open(filename1,'r') as f:
lines = f.readlines()
for line in lines :
value = [float(s) for s in line.split()]
X.append(value[2])
with open(filename2,'r') as f:
lines = f.readlines()
for line in lines :
value = [float(s) for s in line.split()]
Y.append(value[2])
with open(filename3,'r') as f:
lines = f.readlines()
for line in lines :
value = [float(s) for s in line.split()]
Z.append(value[2])
下列代码为处理列循环数据。以每100个数据循环一次为例:
for j in range(100):
A,B,C,D=0,0,0,0
for n in range(20):
m=j+n*100
A=A+X[m]
B=B+Y[m]
C=C+Z[m]
list1.append(A/20) 本例子为将循环的数据求均值
list2.append(B/20)
list3.append(C/20)
list5.append(j)
fileA.write(str(j)+" "+str(A/20)+" "+str(B/20)+" "+str(C/20)+"\n")
以下部分为画图的代码
plt.grid(True)
plt.plot(list5,list1,marker='*',markerfacecolor='red',label='γ=0.2')
plt.plot(list5,list2,marker='o',markerfacecolor='blue',label='γ=0.3')
plt.plot(list5,list3,marker='3',markerfacecolor='green',label='γ=0.5')
plt.xlabel('time'+"\n"+'(d)')
plt.ylabel('number')
plt.legend()
plt.title('the person')
plt.show()
以上代码亲自测试过,确保没有任何问题。