- 积分
- 111
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2016-4-5
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我用python3.4 语言编了一个从A文件中提取温度数据的小程序(程序把txt文件夹里所有的A文件中温度数据都提取出来存在T2004.csv文件里),以下是源代码,与大家共享一下:
import os
def s10(n):
m=("%d" % n)
return m
def str2list(mystr):
mylist = list(mystr.split(' '))
mylist= map(int, mylist)
mylist= map(s10,mylist)
return mylist
def list2str(mylist):
mystr = ','.join(mylist)
mystr = mystr
return mystr
def pathfile(root,fn):
path = ''.join(root)
path = path + "\\"
filename = ''.join(fn)
filename = path + filename
return filename
def l2dd(m):
if m <10:
s = '0' + '%d' %m
else:
s = '%d' %m
return s
def line2d(s):
ss=''
if (s.find('////')>=0):
ss=s.replace('////','9999')
else:
ss=s
return ss
def rep9899(s):
ss=s
if (ss.find('9999')>=0):
ss=ss.replace('9999',' ')
return ss
def opentxt(txtfile,csvfile):
txtf = open(txtfile,'rt')
csvf = open(csvfile,'at')
csvf.seek(0, os.SEEK_END)
content = txtf.readlines()
station = str(content[0][0:5])
yy = str(content[0][28:32])
mm = str(content[0][33:35])
txtlines = len(content)
data = []
pr = 0
tx = ''
for txtline in content:
m = len(txtline)-1
if (txtline[0] == 'T'):
tx = txtline[1]
if (tx == '0') or (tx == '9'):
pr += 1
if pr >0 :
if(pr>1):
dd = l2dd(pr-1)
l = ''
l0 = ''
s = ''
if (txtline[m-1] == '='):
n = m-1
pr = -2
l = list(str2list(line2d(txtline[0:n])))
l0 = txtline[0:n]
else:
l = list(str2list(line2d(txtline[0:m])))
l0 = txtline[0:m]
l = rep9899(list2str(l))
if (tx == '0'):
s = station + ',' + yy+mm+dd + ',' + l + ',' + list2str(l0.split(' '))+"\n"
elif (tx == '9'):
s = station + ',' + yy+mm+dd + ',' + ',' + l + ',' + ',' +list2str(l0.split(' '))+"\n"
csvf.write(s)
pr += 1
txtf.close()
csvf.close()
for root, dirs, files in os.walk("txt"):
for fn in files:
fname = pathfile(root,fn)
opentxt(fname,'T2004.csv')
print(fn)
print('ok')
|
|