Python进度条如何制作,长尾词,代码运行不再无聊?

2026-04-20 05:492阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计500个文字,预计阅读时间需要2分钟。

Python进度条如何制作,长尾词,代码运行不再无聊?

一、普通进度条

pythonimport sysimport time

Python进度条如何制作,长尾词,代码运行不再无聊?

def print_progress_bar(iteration, total, prefix='', suffix='', decimals=1, length=50, fill='█'): percent=({0:. + str(decimals) + f}).format(100 * (iteration / float(total))) filled_length=int(length * iteration // total) bar=fill * filled_length + '-' * (length - filled_length) sys.stdout.write(f'\r{prefix} |{bar}| {percent}% {suffix}') sys.stdout.flush()

示例使用for i in range(101): time.sleep(0.1) print_progress_bar(i + 1, 100)

一、普通进度条

import sys import time # 普通进度条 # 在代码迭代运行中进行统计计算,并使用格式化字符串输出代码运行进度 def progress_bar(): for i in range(1, 101): # 1-100 print("\r", end="") # \r 表示将光标的位置回退到本行的开头位置 print("Download progress: {} {}%".format("▋" * (i // 2), i), end="") sys.stdout.flush() # sys.stdout.flush()的作用就是显示地让缓冲区的内容输出。 time.sleep(0.02) progress_bar()

运行效果如下:

二、带时间的进度条

import time scale = 50 start = time.perf_counter() # 返回性能计数器的值(以秒为单位) for i in range(scale + 1): progress_bar = "▋" * i completion_ratio = (i / scale) * 100 dur_time = time.perf_counter() - start print("\rDownload Process:{:^3.0f}% [{}] {:.2f}s".format(completion_ratio, progress_bar, dur_time),end = "") time.sleep(0.1)

运行效果如下:

三、利用tpdm生成进度条

from time import sleep from tqdm import tqdm # 里面传入一个可迭代对象 for i in tqdm(range(1, 101)): # 模拟你的任务 sleep(0.05) sleep(0.5)

运行效果如下:

四、利用progress生成进度条

import time import progressbar def custom_len(value): # These characters take up more space characters = { '进': 3, '度': 3, } total = 0 for c in value: total += characters.get(c, 1) return total bar = progressbar.ProgressBar( widgets=[ '进度: ', progressbar.Bar(marker='#', left='|', right='|'), ' ', progressbar.Counter(format='%(value)02d/%(max_value)d'), ], len_func=custom_len, ) for i in bar(range(100)): time.sleep(0.05)

运行效果如下:

到此这篇关于Python制作运行进度条的实现效果(代码运行不无聊)的文章就介绍到这了,更多相关Python运行进度条 内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计500个文字,预计阅读时间需要2分钟。

Python进度条如何制作,长尾词,代码运行不再无聊?

一、普通进度条

pythonimport sysimport time

Python进度条如何制作,长尾词,代码运行不再无聊?

def print_progress_bar(iteration, total, prefix='', suffix='', decimals=1, length=50, fill='█'): percent=({0:. + str(decimals) + f}).format(100 * (iteration / float(total))) filled_length=int(length * iteration // total) bar=fill * filled_length + '-' * (length - filled_length) sys.stdout.write(f'\r{prefix} |{bar}| {percent}% {suffix}') sys.stdout.flush()

示例使用for i in range(101): time.sleep(0.1) print_progress_bar(i + 1, 100)

一、普通进度条

import sys import time # 普通进度条 # 在代码迭代运行中进行统计计算,并使用格式化字符串输出代码运行进度 def progress_bar(): for i in range(1, 101): # 1-100 print("\r", end="") # \r 表示将光标的位置回退到本行的开头位置 print("Download progress: {} {}%".format("▋" * (i // 2), i), end="") sys.stdout.flush() # sys.stdout.flush()的作用就是显示地让缓冲区的内容输出。 time.sleep(0.02) progress_bar()

运行效果如下:

二、带时间的进度条

import time scale = 50 start = time.perf_counter() # 返回性能计数器的值(以秒为单位) for i in range(scale + 1): progress_bar = "▋" * i completion_ratio = (i / scale) * 100 dur_time = time.perf_counter() - start print("\rDownload Process:{:^3.0f}% [{}] {:.2f}s".format(completion_ratio, progress_bar, dur_time),end = "") time.sleep(0.1)

运行效果如下:

三、利用tpdm生成进度条

from time import sleep from tqdm import tqdm # 里面传入一个可迭代对象 for i in tqdm(range(1, 101)): # 模拟你的任务 sleep(0.05) sleep(0.5)

运行效果如下:

四、利用progress生成进度条

import time import progressbar def custom_len(value): # These characters take up more space characters = { '进': 3, '度': 3, } total = 0 for c in value: total += characters.get(c, 1) return total bar = progressbar.ProgressBar( widgets=[ '进度: ', progressbar.Bar(marker='#', left='|', right='|'), ' ', progressbar.Counter(format='%(value)02d/%(max_value)d'), ], len_func=custom_len, ) for i in bar(range(100)): time.sleep(0.05)

运行效果如下:

到此这篇关于Python制作运行进度条的实现效果(代码运行不无聊)的文章就介绍到这了,更多相关Python运行进度条 内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!