请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 8962|回复: 0

[源代码] 2000年以前A文件中湿度数据提取

[复制链接]

新浪微博达人勋

发表于 2017-2-21 17:02:33 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

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



密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表