【问题标题】:Python enumerate() tqdm progress-bar when reading a file?Python enumerate() tqdm 读取文件时的进度条?
【发布时间】:2018-07-04 08:47:52
【问题描述】:

当我使用此代码迭代打开的文件时,我看不到 tqdm 进度条:

        with open(file_path, 'r') as f:
        for i, line in enumerate(tqdm(f)):
            if i >= start and i <= end:
                print("line #: %s" % i)
                for i in tqdm(range(0, line_size, batch_size)):
                    # pause if find a file naed pause at the currend dir
                    re_batch = {}
                    for j in range(batch_size):
                        re_batch[j] = re.search(line, last_span)

在这里使用 tqdm 的正确方法是什么?

【问题讨论】:

标签: python progress-bar enumerate tqdm


【解决方案1】:

你在正确的轨道上。您正确使用了 tqdm,但在使用 tqdm 时没有打印循环内的每一行。您还需要在您的第一个 for 循环中使用 tqdm,而不是在其他循环中使用,如下所示:

with open(file_path, 'r') as f:
    for i, line in enumerate(tqdm(f)):
        if i >= start and i <= end:
            for i in range(0, line_size, batch_size):
                # pause if find a file naed pause at the currend dir
                re_batch = {}
                for j in range(batch_size):
                    re_batch[j] = re.search(line, last_span)

关于使用 enumerate 的一些注意事项及其在 tqdm here 中的用法。

【讨论】:

    【解决方案2】:

    我也遇到了这个问题 - tqdm 没有显示进度条,因为没有提供文件对象中的行数。

    for 循环将遍历行,读取直到遇到下一个换行符。

    为了将进度条添加到tqdm,您首先需要扫描文件并计算行数,然后将其作为total 传递给tqdm

    from tqdm import tqdm
    
    num_lines = sum(1 for line in open('myfile.txt','r'))
    with open('myfile.txt','r') as f:
        for line in tqdm(f, total=num_lines):
            print(line)
    

    【讨论】:

      【解决方案3】:

      我正在尝试对包含所有 Wikipedia 文章的文件执行相同的操作。所以我不想在开始处理之前计算总行数。它也是一个 bz2 压缩文件,所以解压缩行的 len 高估了在该迭代中读取的字节数,所以...

      with tqdm(total=Path(filepath).stat().st_size) as pbar:
          with bz2.open(filepath) as fin:
              for line in fin:
                  pbar.update(fin.tell() - pbar.n)
          
          # used this to figure out the attributes of the pbar instance
          # print(vars(pbar))
      

      感谢Yohan Kuanke 删除的答案。如果版主取消删除它,你可以抄袭我的。

      【讨论】:

        【解决方案4】:

        在读取带有readlines()的文件的情况下,可以使用如下:

        from tqdm import tqdm
        with open(filename) as f:
            sentences = tqdm(f.readlines(),unit='MB')
        

        unit='MB' 可以相应地更改为“B”或“KB”或“GB”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-02
          • 2018-01-09
          相关资源
          最近更新 更多