【发布时间】: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