【问题标题】:Is there a way to save TQDM console output to a file?有没有办法将 TQDM 控制台输出保存到文件中?
【发布时间】:2021-10-14 19:03:08
【问题描述】:

我一直在尝试将 tqdm Python 库中的 tqdm 进度条保存到文本文件中。我尝试将 sys.stdoutsys.stderr 重定向到一个文件:

但是,只有 stdout 的输出被保存(例如打印语句),而不是 tqdm 进度条。进度条保留在控制台中。

【问题讨论】:

  • 始终将代码、数据和完整的错误消息作为文本(不是屏幕截图,不是链接)放在有问题的地方(不在评论中)。
  • 您不必打开同一个文件两次 - 您可以将同一个文件 file handler 分配给 stdoutstderr。在某些系统上,如果您尝试使用两个不同的file handlers 写入同一个文件,甚至可能会出错。
  • 我想知道您为什么要将其重定向到文件。 tqdm 使用特殊字符 \r 移动到行首并重新打印新文本,但在文件中你会看到 \r 和所有文本 - 新旧 - 用于进度条的每次更改 - 类似于 [# ]\r[## ]\r[### ]\r[####]。如果您需要来自tqdm 的一些信息,那么也许您应该指定callback,它将使用当前值定期执行,然后您可以将其保存在文件中而无需重定向。
  • 如果我使用重定向 sys.stderr 运行 tqdm,那么我会将其保存在文件中。也许问题不同。也许tqdm 在不同的时刻(在不同的模块中)得到sys,它得到原始值sys.stderr。更好地为您的问题创建最少的工作代码 - 然后我们可以复制并运行它以查看问题并测试一些想法。
  • 感谢您的建议,我刚开始在 Stack Overflow 上提问。我正在运行一些模拟,除了我的程序输出的其他内容之外,我还想记录 tqdm 提供的时间统计信息(迭代次数/秒)。当您使用 sys.stderr 重定向文件时,您是否在文件中获得 tqdm 进度条?

标签: python console tqdm


【解决方案1】:

如果我将sys.stderr 重定向到文件,那么我会在文件中得到tqdm

import tqdm
import time
import sys

fh = open('output.txt', 'w')  # one file for both `stdout` and `stderr`

original_stderr = sys.stderr
sys.stderr = fh
#original_stdout = sys.stdout
#sys.stdout = fh

items = 100

for i in tqdm.tqdm(range(items)):
    time.sleep(0.1)

sys.stderr = original_stderr
#sys.stdout = original_stdout

fh.close()

或者我可以使用tqdm(..., file=fh)

import tqdm
import time

fh = open('output.txt', 'w')

items = 100

for i in tqdm.tqdm(range(items), file=fh):
    time.sleep(0.1)

fh.close()

但是这个文件有这样的东西

  0%|          | 0/100 [00:00<?, ?it/s]
  1%|          | 1/100 [00:00<00:09,  9.98it/s]
  2%|▏         | 2/100 [00:00<00:09,  9.84it/s]
  3%|▎         | 3/100 [00:00<00:09,  9.87it/s]
  4%|▍         | 4/100 [00:00<00:09,  9.90it/s]
  5%|▌         | 5/100 [00:00<00:09,  9.92it/s]
  6%|▌         | 6/100 [00:00<00:09,  9.93it/s]
  7%|▋         | 7/100 [00:00<00:09,  9.93it/s]
  8%|▊         | 8/100 [00:00<00:09,  9.94it/s]
  9%|▉         | 9/100 [00:00<00:09,  9.94it/s]
 10%|█         | 10/100 [00:01<00:09,  9.94it/s]
 11%|█         | 11/100 [00:01<00:08,  9.94it/s]
 12%|█▏        | 12/100 [00:01<00:08,  9.94it/s]
 13%|█▎        | 13/100 [00:01<00:08,  9.95it/s]
 14%|█▍        | 14/100 [00:01<00:08,  9.95it/s]
 15%|█▌        | 15/100 [00:01<00:08,  9.95it/s]
 16%|█▌        | 16/100 [00:01<00:08,  9.94it/s]
 17%|█▋        | 17/100 [00:01<00:08,  9.95it/s]
 18%|█▊        | 18/100 [00:01<00:08,  9.95it/s]
 19%|█▉        | 19/100 [00:01<00:08,  9.95it/s]
 20%|██        | 20/100 [00:02<00:08,  9.95it/s]
 21%|██        | 21/100 [00:02<00:07,  9.95it/s]

tqdm 使用 char \r 移动到行首并打印新文本。
我在 Linux 上的编辑器将其显示为新行,但在 Windows 上,您可能会在一行中看到所有内容,例如

  0%|          | 0/100 [00:00<?, ?it/s]\r  1%|          | 1/100 [00:00<00:09,  9.98it/s]\r  2%|▏         | 2/100 [00:00<00:09,  9.84it/s]

如果您想获得最终值it/s,那么您宁愿使用time.time() 并手动计算。

import tqdm
import time
import sys

items = 100

start = time.time()

for i in tqdm.tqdm(range(items)):
    time.sleep(0.1)

end = time.time()

diff = end-start
items_per_second = items/diff

print(f'time: {diff:.2f} s | {items_per_second:.2f} it/s')

结果:

time: 10.09 s | 9.91 it/s

当您在控制台/终端中运行代码时,您可以使用fh.write()print(..., file=fh) 或重定向sys.stdout 或重定向来写入文件

python script.py > output.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多