其他分享
首页 > 其他分享> > 向文件中写入一万行数据

向文件中写入一万行数据

作者:互联网

  实际工作涉及到代码要对大批量数据文件进行读取下载入库,因此需要测试自己写一个大批量数据文件,然后把文件上传到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 += '\n' + line01
        line02 = str(etc[i][0]) + ',' + str(etc[i][1]) +',10.00,' + str(arr)
        seq02 += '\n' + 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 + '\n' + 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 + '\n' + 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 += '\n' + line01 line02 = str(etc[i][0]) + ',' + str(etc[i][1]) +',10.00,' + str(arr) seq02 += '\n' + 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 + '\n' + 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 + '\n' + md02 flie02.writelines(etc_trans) flie01.close() flie02.close() etcconsumer() etcwsconsumer()

  

 

标签:文件,01,一万,写入,etc,str,date,trans,consumer
来源: https://www.cnblogs.com/mtfan01/p/12752379.html