#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
try:
infilename = sys.argv[1]
outfilename = sys.argv[2]
except:
print "Usage:", sys.argv[0], "infile outfiel"
sys.exit(1)
ifile = open( infilename, 'r')
lines = ifile.readlines()
ofile = open(outfilename, 'w')
x = []
y = []
z = []
Type = ['0','1','2','3','4','5','6','7','8','9']
# 需要把原始数据文档的前两行数字注释掉再处理
for line in lines:
if line.startswith('d)='): # 匹配"d)="的行处理
xval = line.split('=')[1]
x.append(float(xval))
else:
for type in Type:
if line.startswith(type): # 匹配开头是数字的行处理
yval = line.split()[0]
zval = line.split()[1]
y.append(float(yval))
z.append(float(zval))
ofile.write('%s %s %s\n' % ("d)=","Obs","Mean")) # 不需要可以注释掉
for i in range (0,len(x),1):
ofile.write('%g %g %.4f\n' % (x[i],y[i],z[i]))
ifile.close()
ofile.close()
# 刚开始学python,准备用来作数据前后处理
# 正好拿这个作为练习作业了
# 由于缺少很多自动判断,正则写的也不好,所以处理前需要手动处理一下原始数据
# 使得原始数据从 第一个 "d)=" 开始,比如把头两行注释掉
# 以免头两行的数据被读入数组
# 而且需要数据对完整,否则就报错
# 运行结果:
# d)= Obs Mean
# 0.014 34811 135.3840
# 0.015 37928 136.9504
# 0.016 39844 142.5111
# 0.017 40929 145.8832
# 后面的 d)= 0.018,数据不完整,没成对,删掉才能运行通过