pydoc.render_doc() adds characters - how to avoid

2019-07-20 16:59发布

问题:

There are already some questions touching this but no one seems to actually solve it.

import pydoc
hlpTxt = pydoc.render_doc(help)

already does what I want! looks flawless when printed to the (right) console but it has those extra characters included:

_\x08_H\x08He\x08el\x08lp\x08pe\x08er\x08r

In Maya for instance it looks like its filled up with -symbols! While help() renders it flawless as well.

Removing \x08 leaves me with an extra letter each:

__HHeellppeerr

which is also not very useful. Someone commented that it works for him when piped to a subprocess or into a file. I also failed to do that already. Is there another way than

hlpFile = open('c:/help.txt', 'w')
hlpFile.write(hlpTxt)
hlpFile.close()

? Because this leaves me with the same problem. Notepad++ actually shows BS symbols at the places. Yes for backspace obwiously.

Anyway: There must be a reason that these symbols are added and removing them afterwards might work but I can't imagine there isn't a way to have them not created in the first place!

So finally is there another pydoc method I'm missing? Or a str.encode/decode thing I have not yet seen?

btw: I'm not looking for help.__doc__!

回答1:

In python 2, you can remove the boldface sequences with pydoc.plain:

pydoc.plain(pydoc.render_doc(help))

 

>>> help(pydoc.plain)
Help on function plain in module pydoc:

plain(text)
    Remove boldface formatting from text.

 

In python 3 pydoc.render_doc accepts a renderer:

pydoc.render_doc(help, renderer=pydoc.plaintext)