【问题标题】:How can I pass the results of Python's help('modules') output to pbcopy in one line?如何在一行中将 Python 的 help('modules') 输出结果传递给 pbcopy?
【发布时间】:2014-05-28 18:00:49
【问题描述】:

就像标题地址一样,如何做到这一点?我愚蠢地尝试了以下方法,但我将在这里分享愚蠢之处,以便您了解我想要发生的事情:

myself$ python help('modules') | pbcopy


这是个好主意吗:

fout = open('output.txt', 'w')
fout.write(help('modules'))

【问题讨论】:

    标签: python macos unix terminal copy-paste


    【解决方案1】:

    在我的 Ubuntu 上,也希望在你的 boxen 上(因为它是标准的 python 功能),有方便的pydoc 命令,因此很容易输入

    pydoc modules | pbcopy
    

    【讨论】:

      【解决方案2】:

      使用pydoc 查找文档并打印。

      例子:

      $ python -c 'import pydoc; print pydoc.getdoc(id)'
      
      id(object) -> integer
      
      Return the identity of an object.  This is guaranteed to be unique among
      simultaneously existing objects.  (Hint: it's the object's memory address.)
      

      【讨论】:

        【解决方案3】:

        我不知道什么是 pbcopy,但我想这会解决问题:

         python -c 'import urllib; help(urllib)' | pbcopy 
        

        至少这绝对有效:

         python -c 'import urllib; help(urllib)' > file
        

        来自man python

         -c command
                  Specify the command to execute (see next section).  This terminates the option list (following options are passed as arguments to the command).
        

        更新:

        要将其复制到剪贴板,您可以将其添加到~/.bashrc

        pc() { python -c "import $1; help($1);" | xclip -i -selection clipboard; }
        

        然后拨打pc loggingpc my_module

        或者您可以将其通过管道传输到 pbcopy 或任何适合您的方式。

        【讨论】:

        • pbcopy 允许您将输出到控制台的管道传输到剪贴板。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多