• 生成pcf文件


    import os
    import datetime
    import hashlib
    
    
    def checksum(filename):
        with open(filename, mode='rb') as f:
            d = hashlib.md5()
            while True:
                buf = f.read(128)
                if not buf:
                    break
                d.update(buf)
            return d.hexdigest()
    
    if __name__ == "__main__":
    
        week = int(datetime.date.today().strftime("%w"))
    
        if week in {6, 0}:
            print(week)
    
        nextdate = datetime.date.today() + datetime.timedelta(days=1)
        if week == 5:
            nextdate = datetime.date.today() + datetime.timedelta(days=3)
    
        PreTradingDay = datetime.date.today().strftime('%Y%m%d')
        TradingDay = nextdate.strftime('%Y%m%d')
    
        #PreTradingDay = '20191030'
        #TradingDay = '20191031'
    
        print(PreTradingDay)
        print(TradingDay)
    
        path = 'D:/data/pcf'
        template = 'fm006etfd20191104001.txt'
        pcftemp = os.path.join(path, template)
    
        template = 'fm006etfd20191104001.flg'
        flagtemp = os.path.join(path, template)
    
        pcfname = 'fm006etfd' + TradingDay +'001.txt'
        flagname = 'fm006etfd' + TradingDay + '001.flg'
    
        pcffile = os.path.join(path, pcfname)
        flagfile = os.path.join(path, flagname)
    
        print(pcffile)
        print(flagfile)
    
    
        f = open(pcftemp, mode='r', encoding='GBK',newline='
    ')
        f_new = open(pcffile, mode="w+", encoding='GBK',newline='
    ')
    
        for line in f:
            if '20191104' in line:
                line = line.replace('20191104', TradingDay)
                line = line.replace('20191101', PreTradingDay)
            f_new.write(line)
            f_new.flush()
        f.close()
        f_new.close()
    
        stat = os.stat(pcffile)
        FileName = pcfname
        FileDate = str(TradingDay)
        FileTime = str(datetime.datetime.fromtimestamp(stat.st_mtime).strftime("%H%M%S"))
        FileBytes = str(stat.st_size)
        CheckSum = checksum(pcffile)
    
        f = open(flagtemp, mode='r', encoding='GBK',newline='
    ')
        f_new = open(flagfile, mode="w+", encoding='GBK',newline='
    ')
    
        for line in f:
            if 'fm006etfd20191104001' in line:
                line = line.replace('fm006etfd20191104001.txt', FileName)
                line = line.replace('2945', FileBytes)
                line = line.replace('20191104', FileDate)
                line = line.replace('210000', FileTime)
                line = line.replace('5362833CC3437FEC33E69D0FD0287176', CheckSum)
                print(line)
    
            f_new.write(line)
            f_new.flush()
        f.close()
        f_new.close()
        
    

      

  • 相关阅读:
    装饰器 、迭代器,json,pickle,hash
    装饰器知识
    python 编码问题处理
    大数据组件服务的启动与关闭命令
    网站数据统计分析之一:日志收集原理及其实现
    style资源搜索
    分享5个超实用的办公资源网站,错过就可惜了!
    资源搜索
    七大顶级资源
    hive工具bin下的schematool的作用
  • 原文地址:https://www.cnblogs.com/timlong/p/11850920.html
Copyright © 2020-2023  润新知