【问题标题】:How do I update a tqdm progress bar with line size?如何使用行大小更新 tqdm 进度条?
【发布时间】:2017-01-17 04:24:48
【问题描述】:

我正在尝试在 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,但我试图按照评论中所说的去做。

【问题讨论】:

    标签: python tqdm


    【解决方案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))
    

    【讨论】:

    • 请注意,这只适用于二进制文件。
    猜你喜欢
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多