[英]How do I update a tqdm progress bar with line size?
我正在嘗試在Python2.7(Ubuntu 16.04)中加載文件,並使用tqdm顯示當前進度:
from tqdm import tqdm
import os
with open(filename, 'r') as f:
vectors = {}
tq = tqdm(f, total=os.path.getsize(filename))
for line in tq:
vals = line.rstrip().split(' ')
vectors[vals[0]] = np.array([float(x) for x in vals[1:]])
tq.update(len(line))
但是它無法正常工作,因為ETA太大了。 這個示例有點類似,但是我正按照注釋中的說明進行操作。
我發現密鑰沒有將文件對象作為tqdm的“可迭代”參數傳遞,而是手動管理更新:
from tqdm import tqdm
import os
filename = '/home/nate/something.txt'
with open(filename, 'r') as f:
# unit='B' and unit_scale just prettifies the bar a bit
tq = tqdm(total=os.path.getsize(filename), unit='B', unit_scale=True)
for line in f:
tq.update(len(line))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.