您的位置:首页 > 编程语言 > Python开发

最全的Python进度条展示程序方案

2018-02-13 13:53 246 查看
a = 'abc, 123, abc, 123, abc'
b = a.replace('abc','')
print(b)

#2018-02-13 10:53:46 February Tuesday the 07 week, the 044 day SZ SSMR
#使用tqdm进行进度条显示
from tqdm import tqdm
from time import sleep
for i in tqdm(range(10)):
sleep(0.5)

0%|          | 0/10 [00:00<?, ?it/s]
10%|█         | 1/10 [00:00<00:04,  2.00it/s]
20%|██        | 2/10 [00:01<00:04,  2.00it/s]
30%|███       | 3/10 [00:01<00:03,  2.00it/s]
40%|████      | 4/10 [00:02<00:03,  2.00it/s]
50%|█████     | 5/10 [00:02<00:02,  2.00it/s]
60%|██████    | 6/10 [00:03<00:02,  2.00it/s]
70%|███████   | 7/10 [00:03<00:01,  2.00it/s]
80%|████████  | 8/10 [00:04<00:01,  2.00it/s]
90%|█████████ | 9/10 [00:04<00:00,  2.00it/s]
100%|██████████| 10/10 [00:05<00:00,  2.00it/s]
[Finished in 6.2s]

#进度条方法:
#2018-02-13 10:53:42 February Tuesday the 07 week, the 044 day SZ SSMR

import time
N = 10
for i in range(N):
print("进度:{0}%".format(round((i + 1) * 100 / N)), end="\r")
time.sleep(0.1)

进度:10%
进度:20%
进度:30%
进度:40%
进度:50%
进度:60%
进度:70%
进度:80%
进度:90%
进度:100%
[Finished in 1.6s]

#2018-02-13 11:25:08 February Tuesday the 07 week, the 044 day SZ SSMR
# \r 默认表示将输出的内容返回到第一个指针,这样的话,后面的内容会覆盖前面的内容
import time
import sys
def main():

for i in range(65,80):

s="\r{name:s}".format(name=chr(i))

time.sleep(0.05)

sys.stdout.write(s)  #不会有空行
#print(s)               #结果有空行

main()

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O[Finished in 1.5s]

#2018-02-13 11:25:02 February Tuesday the 07 week, the 044 day SZ SSMR
import sys, time

for i in range(10):
sys.stdout.write('{0}/10\r'.format(i + 1))
sys.stdout.flush()
time.sleep(0.1)
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10
[Finished in 1.6s]

0000000000
111111111
22222222
3333333
444444
55555
6666
777
88
9
[Finished in 1.6s]

#2018-02-13 11:26:22 February Tuesday the 07 week, the 044 day SZ SSMR
import sys, time

for i in range(10):
sys.stdout.write(str(i) + '\n' )
sys.stdout.flush()
time.sleep(0.1)

0
1
2
3
4
5
6
7
8
9
[Finished in 1.6s]

#2018-02-13 11:28:42 February Tuesday the 07 week, the 044 day SZ SSMR

import time
N = 10
for i in range(N):
print("进度:{0}%".format(round((i + 1) * 100 / N)), end="\r")
time.sleep(0.01)

进度:10%
进度:20%
进度:30%
进度:40%
进度:50%
进度:60%
进度:70%
进度:80%
进度:90%
进度:100%
[Finished in 1.2s]
#2018-02-13 13:21:01 February Tuesday the 07 week, the 044 day SZ SSMR

进度:10%,已耗时:0.0s,预计剩余时间:0.0s
进度:20%,已耗时:0.01s,预计剩余时间:0.04s
进度:30%,已耗时:0.02s,预计剩余时间:0.05s
进度:40%,已耗时:0.03s,预计剩余时间:0.04s
进度:50%,已耗时:0.04s,预计剩余时间:0.04s
进度:60%,已耗时:0.05s,预计剩余时间:0.03s
进度:70%,已耗时:0.06s,预计剩余时间:0.03s
进度:80%,已耗时:0.07s,预计剩余时间:0.02s
进度:90%,已耗时:0.08s,预计剩余时间:0.01s
进度:100%,已耗时:0.09s,预计剩余时间:-0.0s
[Finished in 0.7s]

import time
N = 10
st = time.clock()
for i in range(N):
p = round((i + 1) * 100 / N)        #round() 方法返回浮点数x的四舍五入值
duration = round(time.clock() - st, 2)
remaining = round(duration * 100 / (0.01 + p) - duration, 2)
print("进度:{0}%,已耗时:{1}s,预计剩余时间:{2}s".format(p, duration, remaining), end = '\r')
time.sleep(0.01)

进度:10%,已耗时:0.0s,预计剩余时间:0.0s
进度:20%,已耗时:0.01s,预计剩余时间:0.04s
进度:30%,已耗时:0.02s,预计剩余时间:0.05s
进度:40%,已耗时:0.03s,预计剩余时间:0.04s
进度:50%,已耗时:0.04s,预计剩余时间:0.04s
进度:60%,已耗时:0.05s,预计剩余时间:0.03s
进度:70%,已耗时:0.06s,预计剩余时间:0.03s
进度:80%,已耗时:0.07s,预计剩余时间:0.02s
进度:90%,已耗时:0.08s,预计剩余时间:0.01s
进度:100%,已耗时:0.09s,预计剩余时间:-0.0s
[Finished in 0.7s]

#!/usr/bin/python
import time

def procedure():
time.sleep(2.5)

# measure process time
t0 = time.clock()
procedure()
print(time.clock() - t0, "seconds process time")

# measure wall time
t0 = time.time()
procedure()
print(time.time() - t0, "seconds wall time")

import time

print("time.time(): %f " %  time.time())
print(time.localtime( time.time() ))
print(time.asctime( time.localtime(time.time()) ))

time.time(): 1518500152.606349
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=13, tm_hour=13, tm_min=35, tm_sec=52, tm_wday=1, tm_yday=44, tm_isdst=0)
Tue Feb 13 13:35:52 2018
[Finished in 0.7s]

import time
import progressbar
p = progressbar.ProgressBar()
N = 10
for i in p(range(N)):
time.sleep(0.01)
#2018-02-13 13:51:06 February Tuesday the 07 week, the 044 day SZ SSMR
import time
import progressbar
p = progressbar.ProgressBar()
N = 1000
p.start(N)
for i in range(N):
time.sleep(0.01)
p.update(i+1)
p.finish()

#2018-02-13 13:51:13 February Tuesday the 07 week, the 044 day SZ SSMR

import time
import progressbar
bar = progressbar.ProgressBar(widgets=[
' [', progressbar.Timer(), '] ',
progressbar.Percentage(),
' (', progressbar.ETA(), ') ',
])
for i in bar(range(1000)):
time.sleep(0.01)

'Timer',          # 计时器
'ETA',            # 预计剩余时间
'AbsoluteETA',    # 预计结束的绝对时间,耗时很长时使用较方便
'Percentage',     # 百分比进度,30%
'SimpleProgress', # 计数进度,300/1000
'Counter',        # 单纯计数
'Bar'             # “#”号进度条
#2018-02-13 13:51:33 February Tuesday the 07 week, the 044 day SZ SSMR
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: