如何导出 Python 内置 help() 函数的输出



我有一个python包,它输出了大量的帮助文本: help(package)

我想将此帮助文本导出到文件中,其显示格式为 help(package)

我该怎么做呢?

pydoc.render_doc(thing) 以字符串形式获取事物的帮助文本。 pydoc 的其他部分,如 pydoc.text 和 pydoc.html 可以帮助你将其写入文件。

例如,在 linux 中使用 -w 修饰符会将输出写入当前目录中的 html;

pydoc -w Rpi.GPIO

将从命令help(Rpi.GPIO)呈现的所有help()文本放入格式良好的文件 Rpi.GPIO.html 中,该文件位于 shell 的当前目录中

这有点黑客(在某个地方可能有更好的解决方案),但这有效:

import sys
import pydoc
def output_help_to_file(filepath, request):
    f = open(filepath, 'w')
    sys.stdout = f
    pydoc.help(request)
    f.close()
    sys.stdout = sys.__stdout__
    return

然后。。。

>>> output_help_to_file(r'test.txt', 're')

一个老问题,但较新的推荐通用解决方案(适用于Python 3.4+)用于写入print()终端的函数输出,使用contextlib.redirect_stdout

import contextlib
def write_help(func, out_file):
    with open(out_file, 'w') as f:
        with contextlib.redirect_stdout(f):
            help(func)

使用示例:

write_help(int, 'test.txt')

要获得"干净"的文本输出,就像内置的help()一样,并且适合导出到文件或其他任何内容,您可以使用以下内容:

>>> import pydoc
>>> pydoc.render_doc(len, renderer=pydoc.plaintext)
'Python Library Documentation: built-in function len in module builtinsnnlen(obj, /)n    Return the number of items in a container.n'

如果你做帮助(帮助),你会看到:

Help on _Helper in module site object:
class _Helper(__builtin__.object)
 |  Define the builtin 'help'.
 |  This is a wrapper around pydoc.help (with a twist).

[休息剪断]

所以 - 你应该看看 pydoc 模块 - 会有一个或多个方法返回help(something)作为字符串所做的......

选定的答案对我不起作用,所以我做了更多的搜索,找到了在Daniweb上工作的东西。功劳归于拉斯维加斯。https://www.daniweb.com/programming/software-development/threads/20774/starting-python/8#post1306519

# simplified version of sending help() output to a file
import sys
# save present stdout
out = sys.stdout
fname = "help_print7.txt"
# set stdout to file handle
sys.stdout = open(fname, "w")
# run your help code
# its console output goes to the file now
help("print")
sys.stdout.close()
# reset stdout
sys.stdout = out

最简单的方法是使用

系统模块

它在操作系统和自身之间打开数据流,它从帮助模块中获取数据,然后将其保存在外部文件中

file="str.txt";file1="list.txt"
out=sys.stdout
sys.stdout=open('str_document','w')
help(str)
sys.stdout.close

最干净的方式

假设help(os)

步骤 1 - 在 Python 控制台中

 import pydoc 
 pydoc.render_doc(os, renderer=pydoc.plaintext)` 
 
 #this will display a string containing help(os) output 

步骤 2 - 复制字符串

步骤 3 - 在终端上

echo "copied string" | tee somefile.txt 

如果要在文本文件中写入类信息。请按照以下步骤操作

  1. 在类和运行文件中的某个位置插入 pdb 钩子

    import pdb; pdb.set_trace()

  2. 执行上述步骤 1 到 3

在Windows中,只需打开一个Windows命令行窗口,转到Python安装的Lib子文件夹,然后键入

python pydoc.py moduleName.memberName> c:\myFolder\memberName.txt

将模块名称中的属性或方法成员名称的文档放入文件成员名称.txt中。 如果你想要一个对象在模块的层次结构中更靠后,只需放置更多的点。 例如

python pydoc.py wx.lib.agw.ultimatelistctrl> c:\myFolder\UltimateListCtrl.txt

将 wxPython 包中 agw 包中的 UltimateListCtrl 控件上的文档放入 UltimateListCtrl.txt。

pydoc 已经提供了所需的功能,这是所有问答系统都应该具备的一个设计非常好的功能。 皮多克。助手。init 有一个输出对象,所有输出都发送到那里。 如果使用自己的输出对象,则可以执行任何操作。 例如:

类输出():

def __init__(self):
    self.results = []
def write(self,text):
    self.results += [text]
def flush(self):
    pass
def print_(self):
    for x in self.results: print(x)
def return_(self):
    return self.results
def clear_(self):
    self.results = []

当传递为

O = OUTPUT() # 必须记住结果,但见下文。

帮助 = 皮多克。助手(O)

将所有结果存储在输出实例中。当然,以 O = OUTPUT() 开头并不是最好的主意(见下文)。 render_doc不是中心输出点;输出是。 我想要输出,这样我就可以防止大输出从屏幕上消失,比如马克·卢茨(Mark Lutz)的"更多"。 不同的输出将允许您写入文件。

您还可以在类 pydoc 的末尾添加"返回"。用于返回所需信息的帮助程序。 像这样:

如果self.output_:返回self.output_

应该工作,或

如果self.output_:返回 self.output.return_()

所有这些都是可能的,因为 pydoc 设计得很好。 它是隐藏的,因为帮助的定义省略了输入和输出参数。

使用命令行,我们可以直接获取输出并将其传送到任何有用的输出。

python -m pydoc ./my_module_file.py

-- ./很重要,它告诉 pydoc 查看您的本地文件,而不是尝试从其他地方导入。

如果您在Mac上,则可以将输出通过管道传输到pbcopy并将其粘贴到您选择的文档工具中。

python -m pydoc ./my_module_file.py | pbcopy

只需编写一个小脚本来调用要捕获的help()并通过输出重定向从命令行运行它。

最新更新