邀人做任务比较好的发布网站/淘宝关键词排名
一、Python 实现进度条
考虑到进度条输出的灵活性,最终是以文本形式返回而不是直接打印。
这样进度条和文字表达连接更加自由
# python 3.6
# author: Scc_hy
# create date: 2019-08-20
# Function: 列表遍历进度条
# file_name: progressingclass Progress():"""对列表遍历,返回对应遍历进度 param lsit_i: list param width: int 进度条长度 例子:m = ['asdas', 'asdas','qwe124', 'asd112', '12asdx', '12asdx', '12asdx']p = Progress(m, width = 45)for i in m:msg = p.progress()print('目前打印: {}, 进度: {}'.format(i, msg))"""def __init__(self, list_i, width = 25):self.list_i = list_iself.list_long = len(list_i)self.width = widthself.start = 0def get_progress(self, percent = 0):"""百分比显示方式 """left = percent * self.width // self.list_longpct = percent * self.width / self.list_longright = self.width - leftleft_now = '#' * leftright_now = ' ' * rightmult = 100 / self.widthreturn "[{}{}] {:.1f}%".format(left_now, right_now, pct * mult)def flush_percent(self):"""刷新开始位置"""self.start += 1def progress(self):"""输出进度条"""if self.start <= self.list_long:self.flush_percent()msg = self.get_progress(self.start)return msgif __name__ == '__main__':m = ['asdas', 'asdas','qwe124', 'asd112', '12asdx', '12asdx', '12asdx']p = Progress(m, width = 45)for i in m:msg = p.progress()print('目前打印: {}, 进度: {}'.format(i, msg))
二、实际场景应用
以下是不完整脚本
import os
import re
import argparse
from datetime import datetime
from progressing import Progressdef Count_Lines(filroot):out_name = 'out_{}.csv'.format(datetime.now().strftime('%Y%d%m'))outfil = os.path.join(filroot, out_name)for root, dirs, files in os.walk(filroot):# 批量读取len_out = 0files_csv = [i for i in files if 'csv' in i and i != out_name]p = Progress(files_csv)for i in files_csv:fil = os.path.join(filroot, i)f = open(fil,'r', encoding='utf-8')f_read = f.read()f.close()# 记录行数 cnt = re.findall(r'\n', f_read, re.S)len_out += len(cnt)msg = p.progress()print('{} 合并文件: {}, {}行.'.format(msg, i, len(cnt)))# 依次写入需要f1 = open(outfil,'a+', encoding='utf-8')words = re.compile("'") # 关键词定位new_f = words.sub('', f_read, re.S) # 替换关键词f1.write(new_f)f1.close()return len_out + 1if __name__ == '__main__' :args = parse_args()filroot = args.base_rootfinal_fil = 'out_{}.csv'.format(datetime.now().strftime('%Y%d%m'))print("最终文件: {}, {}行".format(final_fil, Count_Lines(filroot)))input("\n输入任意字符结束")