• python向文件中写入一万行数据


      实际工作涉及到代码要对大批量数据文件进行读取下载入库,因此需要测试自己写一个大批量数据文件,然后把文件上传到ftp上,然后调用接口下载入库。然后自己看书的时候找到了一个方法,因此尝试的写了这么一段脚,以备后续参考。

    from ETC import etcmysqlconnent as p
    import random,uuid,datetime
    import hashlib
    
    now_time=datetime.datetime.now()
    date = ((now_time+datetime.timedelta(days=-1)).strftime("%Y%m%d"))
    time_01 = ((now_time+datetime.timedelta(hours=-1)).strftime("%H%M%S"))
    time_02 = ((now_time+datetime.timedelta(hours=-2)).strftime("%H%M%S"))
    #f6c416e7e213,0,222201312314342,222201942311343,20200416,133620,20200416,121221,1000,0,201416121
    def etcconsumer():
        db = p.JbcConnect()
        #从数据库查询出一万条数据
        etc = db.etc_accountno()
        arr = 100000000
        count = 0
        sum = 0
        seq01 = ''
        seq02 = ''
        for i in range(len(etc)):
            res = str(uuid.uuid4())
            res = res.replace('-', '')
            arr += 1
            count += 1
            sum += 10.00
            line01 = str(res[:16]) + ',0,' + str(etc[i][0]) + ',' + str(etc[i][1]) + ',' + date + ',' + time_01 + ',' + date + ',' + time_02 + ',10.00,0,' + str(arr)
            #把内容逐行累加
            seq01 += '
    ' + line01
            line02 = str(etc[i][0]) + ',' + str(etc[i][1]) +',10.00,' + str(arr)
            seq02 += '
    ' + line02
        line_01 = date + ',' + str(count) + ',' + str(sum)
        line_02 = str(count) + ',' + str(sum)
        etc_consumer = line_02 + seq02
        etc_trans = line_01 + seq01
        #print(etc_consumer)
        #print(etc_trans)
        consumer = 'ETC_CONSUME_' + date
        flie01 = open('D:/untitled/file/' + consumer, 'w')
        #将文件内容进行MD5加密
        md01 = hashlib.md5()
        md01.update(etc_consumer.encode(encoding='utf-8'))
        md_01 = md01.hexdigest()
        etc_consumer = etc_consumer + '
    ' + md_01
        flie01.writelines(etc_consumer)
        trans = 'ETC_TRANS_' + date
        flie02 = open('D:/untitled/file/' + trans, 'w')
        md02 = hashlib.md5(etc_trans.encode(encoding='UTF-8')).hexdigest()
        etc_trans = etc_trans + '
    ' + md02
      #将一万行数据一次性写入文件中。 flie02.writelines(etc_trans) flie01.close() flie02.close() def etcwsconsumer(): db = p.JbcConnect() etc = db.etc_accountno() arr = 200000000 count = 0 sum = 0 seq01 = '' seq02 = '' for i in range(len(etc)): res = str(uuid.uuid4()) res = res.replace('-', '') arr += 1 count += 1 sum += 10.00 line01 = str(res[:16]) + ',0,' + str(etc[i][0]) + ',' + str(etc[i][1]) + ',' + date + ',' + time_01 + ',' + date + ',' + time_02 + ',10.00,0,' + str(arr) seq01 += ' ' + line01 line02 = str(etc[i][0]) + ',' + str(etc[i][1]) +',10.00,' + str(arr) seq02 += ' ' + line02 line_01 = date + ',' + str(count) + ',' + str(sum) line_02 = str(count) + ',' + str(sum) etc_consumer = line_02 + seq02 etc_trans = line_01 + seq01 #print(etc_consumer) #print(etc_trans) consumer = 'ETC_WSCONSUME_' + date flie01 = open('D:/untitled/file/' + consumer, 'w') md01 = hashlib.md5() md01.update(etc_consumer.encode(encoding='utf-8')) md_01 = md01.hexdigest() etc_consumer = etc_consumer + ' ' + md_01 flie01.writelines(etc_consumer) trans = 'ETC_WSTRANS_' + date flie02 = open('D:/untitled/file/' + trans, 'w') md02 = hashlib.md5(etc_trans.encode(encoding='UTF-8')).hexdigest() etc_trans = etc_trans + ' ' + md02 flie02.writelines(etc_trans) flie01.close() flie02.close() etcconsumer() etcwsconsumer()

      

  • 相关阅读:
    路由交换03-----传输层协议
    Linux结构目录
    Linux RedHat 7 配置本地 YUM源
    HUAWEI,H3C 三层交换机 常用命令
    windows操作系统更改 <远程桌面> 端口号
    SpringBoot 2.x (2):请求和传参
    SpringBoot 2.x (1):手动创建项目与自动创建项目
    Eclipse中使用Maven搭建SSM框架
    基于Spring和Mybatis拦截器实现数据库操作读写分离
    WinServer配置MySQL主从同步
  • 原文地址:https://www.cnblogs.com/mtfan01/p/12752379.html
Copyright © 2020-2023  润新知