读取文件时,Python enumerate() tqdm 进度条? [英] Python enumerate() tqdm progress-bar when reading a file?

查看:519
本文介绍了读取文件时,Python enumerate() tqdm 进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't see the tqdm progress bar when I use this code to iterate my opened file:

        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 的正确方法是什么?

what's the right way to use tqdm here?

推荐答案

您走对了.您正在正确使用 tqdm,但在使用 tqdm 时停止打印循环内的每一行.您还需要在第一个 for 循环而不是其他循环中使用 tqdm,如下所示:

You're on the right track. You're using tqdm correctly, but stop short of printing each line inside the loop when using tqdm. You'll also want to use tqdm on your first for loop and not on others, like so:

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 中的用法 此处.

Some notes on using enumerate and its usage in tqdm here.

这篇关于读取文件时,Python enumerate() tqdm 进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆