Python CSV 文件复制并输出进度 - 优化代码效率
@resource_reference{'/dataset/ods/df_orders.csv'}
import os from datetime import datetime
if 'main' == name: # 定义输入输出文件路径 input_file_name = 'dataset/ods/df_orders.csv' output_file_name = '${bdp.system.savingModelPath}' + 'df_orders_1g.csv'
# 打开输出文件,将输入文件的内容逐行写入输出文件
with open(output_file_name, 'w') as output_file:
with open(input_file_name, 'r') as input_file:
line = input_file.readline()
output_file.writelines(line)
line = input_file.readline()
k = 0
while line:
k += 1
output_file.writelines(line)
line = input_file.readline()
# 每处理100000行,输出一次处理进度
if 0 == k % 100000:
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), k, line, flush=True)
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), k, line, flush=True)
# 输出输入输出文件的大小
print(os.path.getsize(input_file_name)/1024/1024, 'M')
print(os.path.getsize(output_file_name)/1024/1024, 'M')
原文地址: https://www.cveoy.top/t/topic/oe1N 著作权归作者所有。请勿转载和采集!