我正在尝试解析 Python doctest 模块的输出并将其存储在 HTML 文件中。
我有类似这样的输出:
**********************************************************************
File "example.py", line 16, in __main__.factorial
Failed example:
[factorial(n) for n in range(6)]
Expected:
[0, 1, 2, 6, 24, 120]
Got:
[1, 1, 2, 6, 24, 120]
**********************************************************************
File "example.py", line 20, in __main__.factorial
Failed example:
factorial(30)
Expected:
25252859812191058636308480000000L
Got:
265252859812191058636308480000000L
**********************************************************************
1 items had failures:
2 of 8 in __main__.factorial
***Test Failed*** 2 failures.
每个失败之前都有一行星号,用于分隔每个测试失败。
我想做的是去掉失败的文件名和方法,以及预期和实际结果。然后我想使用它创建一个 HTML 文档(或将其存储在一个文本文件中,然后进行第二轮解析)。
如何仅使用 Python 或 UNIX shell 实用程序的某种组合来做到这一点?
编辑:我制定了以下 shell 脚本,它匹配我想要的每个块,但我不确定如何将每个 sed 匹配重定向到它自己的文件。
python example.py | sed -n '/.*/,/^\**$/p' > `mktemp error.XXX`