python 处理多行多列多文件的数据

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()  

以上代码亲自测试过,确保没有任何问题。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值