爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 3784|回复: 0

2000以前A文件里温度数据提取(python实现)

[复制链接]

新浪微博达人勋

发表于 2016-12-19 13:57:52 | 显示全部楼层 |阅读模式

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

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

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


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

本版积分规则

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

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

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