简体   繁体   English

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

[英]How do I export the output of Python's built-in help() function

I've got a python package which outputs considerable help text from: help(package)我有一个 python 包,它输出大量的帮助文本: help(package)

I would like to export this help text to a file, in the format in which it's displayed by help(package)我想将此帮助文本导出到文件中,格式为help(package)

How might I go about this?我该怎么办?

pydoc.render_doc(thing) to get thing's help text as a string. pydoc.render_doc(thing) 以字符串形式获取事物的帮助文本。 Other parts of pydoc like pydoc.text and pydoc.html can help you write it to a file. pydoc 的其他部分如 pydoc.text 和 pydoc.html 可以帮助您将其写入文件。

Using the -w modifier in linux will write the output to a html in the current directory, for example;例如,在 linux 中使用-w修饰符会将输出写入当前目录中的 html;

pydoc -w Rpi.GPIO

Puts all the help() text that would be presented from the command help(Rpi.GPIO) into a nicely formatted file Rpi.GPIO.html, in the current directory of the shell将命令help(Rpi.GPIO)显示的所有help()文本放入一个格式良好的文件 Rpi.GPIO.html 中,位于 shell 的当前目录中

This is a bit hackish (and there's probably a better solution somewhere), but this works:这有点hackish(并且某处可能有更好的解决方案),但这有效:

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

And then...接着...

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

An old question but the newer recommended generic solution (for Python 3.4+) for writing the output of functions that print() to terminal is using contextlib.redirect_stdout :一个老问题,但较新的推荐通用解决方案(适用于 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)

Usage example:用法示例:

write_help(int, 'test.txt')

To get a "clean" text output, just as the built-in help() would deliver, and suitable for exporting to a file or anything else, you can use the following:要获得“干净”的文本输出,就像内置的 help() 将提供的一样,并且适合导出到文件或其他任何内容,您可以使用以下内容:

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

If you do help(help) you'll see:如果你做 help(help) 你会看到:

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).

[rest snipped] [休息剪断]

So - you should be looking at the pydoc module - there's going to be a method or methods that return what help(something) does as a string...所以 - 你应该看看 pydoc 模块 - 将会有一个或多个方法返回help(something)作为字符串的作用......

Selected answer didn't work for me, so I did a little more searching and found something that worked on Daniweb.选定的答案对我不起作用,所以我做了更多的搜索,发现了一些对 Daniweb 有效的东西。 Credit goes to vegaseat.归功于 vegaseat。 https://www.daniweb.com/programming/software-development/threads/20774/starting-python/8#post1306519 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

The simplest way to do that is via using最简单的方法是使用

sys module系统模块

it opens a data stream between the operation system and it's self , it grab the data from the help module then save it in external file它在操作系统和它自己之间打开一个数据流,它从帮助模块中获取数据然后将其保存在外部文件中

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

The cleanest way最干净的方式

Assuming help(os)假设help(os)

Step 1 - In Python Console第 1 步 - 在 Python 控制台中

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

Step 2 - Copy string第 2 步 - 复​​制字符串

Step 3 - On a Terminal第 3 步 - 在终端上

echo "copied string" | tee somefile.txt 

If you want to write Class information in a text file.如果要在文本文件中写入 Class 信息。 Follow below steps按照以下步骤操作

  1. Insert pdb hook somewhere in the Class and run file在类中的某处插入 pdb 钩子并运行文件

    import pdb;导入 pdb; pdb.set_trace() pdb.set_trace()

  2. Perform step 1 to 3 stated above执行上述步骤 1 到 3

In Windows, just open up a Windows Command Line window, go to the Lib subfolder of your Python installation, and type在 Windows 中,只需打开一个 Windows 命令行窗口,转到 Python 安装的 Lib 子文件夹,然后键入

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

to put the documentation for the property or method memberName in moduleName into the file memberName.txt.将 moduleName 中的属性或方法 memberName 的文档放入文件 memberName.txt。 If you want an object further down the hierarchy of the module, just put more dots.如果您想要在模块层次结构中更向下的对象,只需放置更多点即可。 For example例如

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

to put the documentation on the UltimateListCtrl control in the agw package in the wxPython package into UltimateListCtrl.txt.将wxPython包中agw包中UltimateListCtrl控件的文档放到UltimateListCtrl.txt中。

pydoc already provides the needed feature, a very well-designed feature that all question-answering systems should have. pydoc 已经提供了所需的功能,这是所有问答系统都应具备的精心设计的功能。 The pydoc.Helper. pydoc.Helper。 init has an output object, all output being sent there. init有一个输出对象,所有输出都被发送到那里。 If you use your own output object, you can do whatever you want.如果您使用自己的输出对象,则可以为所欲为。 For example:例如:

class OUTPUT():类输出():

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 = []

when passed as当作为

O = OUTPUT() # Necessarily to remember results, but see below. O = OUTPUT() # 必须记住结果,但见下文。

help = pydoc.Helper(O)帮助 = pydoc.Helper(O)

will store all results in the OUTPUT instance.将所有结果存储在 OUTPUT 实例中。 Of course, beginning with O = OUTPUT() is not the best idea (see below).当然,以 O = OUTPUT() 开头并不是最好的主意(见下文)。 render_doc is not the central output point; render_doc 不是中心输出点; output is.输出是。 I wanted OUTPUT so I could keep large outputs from disappearing from the screen using something like Mark Lutz' "More".我想要 OUTPUT,这样我就可以使用 Mark Lutz 的“More”之类的东西来防止大输出从屏幕上消失。 A different OUTPUT would allow you to write to files.不同的 OUTPUT 将允许您写入文件。

You could also add a "return" to the end of the class pydoc.Helper to return the information you want.您还可以在 pydoc.Helper 类的末尾添加“返回”以返回您想要的信息。 Something like:就像是:

if self.output_: return self.output_如果 self.output_:返回 self.output_

should work, or应该工作,或者

if self.output_: return self.output.return_() if self.output_: 返回 self.output.return_()

All of this is possible because pydoc is well-designed.所有这一切都是可能的,因为 pydoc 是精心设计的。 It is hidden because the definition of help leaves out the input and output arguments.它是隐藏的,因为 help 的定义忽略了输入和输出参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以字符串形式获取python中内置help()函数的输出? - How to get the output of the built-in help() function in python in a string? 我如何删除<built-in function array>从输出 - how do i remove <built-in function array> from the output 我如何找到python内置函数实现? - How do I find python built-in function implementations? 在python 3.6中,如何使用交互式提示中的通配符过滤帮助内置函数的输出? - In python 3.6, how to filter output of help built-in function using wildcards from interactive prompt? 如何在python help()内置函数结果中搜索某些单词? - How to search some word in python help() built-in function result? 你如何研究python的内置方法的实现? - How do you investigate python's implementation of built-in methods? 如何在C#中复制Python的已排序内置函数的行为? - How can I replicate the behavior of Python's sorted built-in function in C#? 如何将表示时间的 strng 转换为 Python 中的数字? 有什么内置功能吗? - How do I turn a strng which represents time to a number in Python? Any built-in function? 如何在内置的open()函数中输入文件路径? - How do I enter file path in built-in open() function? 我如何使用all()内置函数? - How do I use all() built-in function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM