pydoc.render_doc() adds characters - how to avoid that?

十年热恋 提交于 2019-12-11 01:39:50

问题


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)

 



来源:https://stackoverflow.com/questions/15133537/pydoc-render-doc-adds-characters-how-to-avoid-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!