|
发表于 2007-6-22 22:31:12
|
显示全部楼层
来段python的,perl不熟悉
来段python的,perl不熟悉,
直接处理数字行,不包括注释行
[PHP]import re
file1=open("1.txt","r")
#读第一个文件中每行数据,并用空格分割每行数据
line1=[re.compile(" +").split(each1) for each1 in file1.readlines()]
file1.close()
count=0
file2=open("2.txt","r")
for line2 in file2.readlines():
count+=1
#打开新文件准备写入
newfile=open("new"+str(count)+".txt","w")
#用空格分割第二个文件当前行
each2=re.compile(" +").split(line2)
for each1 in line1:
if(each1[4]==each2[4]):
#在第一个文件中找到相应的行,并相加输出,用“\t”作为新分割符
newfile.write("%.4f\t%.4f\t%.4f\t%s"%(float(each1[1])+float(each2[1]),float(each1[2])+float(each2[2]),float(each1[3])+float(each2[3]),each1[4]))
else:
#不处理,直接输出
newfile.write("%s\t%s\t%s\t%s"%(each1[1],each1[2],each1[3],each1[4]))
newfile.close()
file2.close()
[/PHP] |
|