【问题标题】:Multiple line text unto clipboard, adding bullets to wiki mark up多行文本到剪贴板,将项目符号添加到 wiki 标记
【发布时间】:2019-02-08 12:10:26
【问题描述】:

我希望将文本打印为练习中的显示方式,其中列表列表的每一行都有一个*,并且每个都在一个新行中。我还是 python 的新手,Automate the Boring Stuff with Python 这本书有时有点令人困惑。

我首先在 Python 编辑器中输入文本并让 Pyperclip 将其复制到剪贴板。问题是 Pyperclip 只接受一个字符串,文本以这种形式复制到剪贴板。

#! python3

#bulletPointerAdder.py - Adds Wikipedia bullet points to the start
#of each line of text on the clipboard.
#! python3

#bulletPointerAdder.py - 将 Wikipedia 项目符号添加到开头 #of 剪贴板上的每一行文本。

在 Python 外壳中:

import pyperclip
>>> text = 'Lists of monkeys Lists of donkeys Lists of pankeys'
>>> pyperclip.copy(text)
>>>
 RESTART: C:\Users\User\AppData\Local\Programs\Python\Python37-

32\bulletpointadder.py >>> 文字 '* 猴子列表 驴列表 pankey 列表' >>>

import os
import pyperclip
text = pyperclip.paste()


#Separate lines and add starts.
lines = text.split(os.linesep)
for i in range(len(lines)): # loop through all indexes in the "lines"
list
    lines[i] = '* ' + lines[i] # add star to each sting in "lines" list

text = os.linesep.join(lines)
pyperclip.copy(text)

我实际上希望像下面的示例一样打印文本,但问题是我将它打印为单个字符串。

  • 动物列表
  • 水族馆生活清单
  • 按作者缩写的生物学家名单
  • 品种列表

【问题讨论】:

    标签: list text newline clipboard pyperclip


    【解决方案1】:

    首先了解这一点,然后转到第 3 步:

    我们沿换行符拆分文本以获得一个列表,其中每个项目都是文本的一行。我们将列表存储在行中,然后循环遍历行中的项目。

    对于每一行,我们在行首添加一个星号和一个空格。现在行中的每个字符串都以星号开头。

    【讨论】:

      【解决方案2】:
      import pyperclip
      
      text = pyperclip.paste()
      
      # TODO manipulate the text in clipboard
      lines = text.split('\n')                        # Each word is split into new line
      for i in range(len(lines)):
          lines[i] = '* ' + lines[i]                  # Each word gets a * prefix
      text = '\n'.join(lines)                         # all the newlines created are joind back
      pyperclip.copy(text)                            # whole content is than copied into clipboard
      print(text)
      

      使用此代码,如果您复制一个事物列表,它仍然是一个预期的事物列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-17
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-23
        相关资源
        最近更新 更多