如何使用行大小更新tqdm进度条?

时间:2017-01-17 04:24:49

标签: python tqdm

我正在尝试在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太大了。它有点follows this example,但我试图像评论中所说的那样做。

1 个答案:

答案 0 :(得分:1)

我发现密钥没有将文件对象作为“可迭代的”传递给对象。 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))
相关问题