- 积分
- 111
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2016-4-5
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
使用Python写了一段小程序,用来提取2000年以前A文件中相对湿度数据,以下是程序源码:
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('%%','100')
elif (s.find('//')>=0):
ss=s.replace('//','-9')
else:
ss=s
return ss
def rep9899(s):
ss=''
if (s.find('-9')>=0):
ss=s.replace('-9',' ')
else:
ss=s
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] == 'U'):
tx = txtline[1]
if (tx == '0') or (tx == '2')or (tx == '7') 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 == '2'):
s = station + ',' + yy+mm+dd + ',' + l + ',' + ',' + list2str(l0.split(' '))+ ','+"\n"
elif (tx == '7'):
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,'U2004.csv')
print('ok')
|
|