python 的 Tqdm 模塊


python的Tqdm模塊


Tqdm 是一個快速,可擴展的Python進度條,可以在 Python 長循環中添加一個進度提示信息,用戶只需要封裝任意的迭代器 tqdm(iterator)。

我的系統是window環境,首先安裝python,接下來就是pip。

pip安裝:

在python根目錄下創建一個get-pip.py的文件,內容:


https://bootstrap.pypa.io/get-pip.py
然后在CMD窗口進入python下面:
輸出:


python -m pip install -U pip
由於Tqdm要求的pip版本是9.0所以需要手動安裝pip9.0
http://pypi.python.org/pypi/pip

下載安裝包9.0

然后解壓進入,CMD窗口輸入:python setup.py install

然后就可以安裝Tqdm了,


pip install tqdm
安裝最新的開發版的話

pip install -e git+https://github.com/tqdm/tqdm.git@master#egg=tqdm
最后看看怎么用呢?https://pypi.python.org/pypi/tqdm
基本用法:


from tqdm import tqdm
for i in tqdm(range(10000)):
sleep(0.01)
當然除了tqdm,還有trange,使用方式完全相同
for i in trange(100):
sleep(0.1)
只要傳入list都可以:

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
pbar.set_description("Processing %s" % char)
也可以手動控制更新
with tqdm(total=100) as pbar:
for i in range(10):
pbar.update(10)
也可以這樣:

pbar = tqdm(total=100)
for i in range(10):
pbar.update(10)
pbar.close()
在Shell的tqdm用法
統計所有python腳本的行數:


$ time find . -name '*.py' -exec cat \{} \; | wc -l
857365

real 0m3.458s
user 0m0.274s
sys 0m3.325s

$ time find . -name '*.py' -exec cat \{} \; | tqdm | wc -l
857366it [00:03, 246471.31it/s]
857365

real 0m3.585s
user 0m0.862s
sys 0m3.358s
使用參數:
$ find . -name '*.py' -exec cat \{} \; |
tqdm --unit loc --unit_scale --total 857366 >> /dev/null
100%|███████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s]
備份一個目錄:
$ 7z a -bd -r backup.7z docs/ | grep Compressing |
tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log
100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s
————————————————
版權聲明:本文為CSDN博主「langb2014」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/langb2014/article/details/54798823

 

=================================

tqdm輸出的含義:

 

 17%|█▋ | 134/782 [00:19<01:21, 7.98it/s, loss=0.375 ] 

The fields in order are:

  • 17%: Percentage complete.
  • |█▋ | : Progress bar
  • 134/782: Number of items iterated over total number of items.
  • [00:19<01:21, 7.98it/s, loss=0.375 ]: Lets break this down below separately.
    • 00:19<01:21 : it refers to {elapsed}<{remaining}.
    • 7.98it/s: iterations per second
    • loss=0.375: As the label says, it is the loss.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM