项目:在 Wiki 标记中添加无序列表
在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占
据一行,并在前面放置一个星号。但是假设你有一个非常大的列表,希望添加前面
的星号。你可以在每一行开始处输入这些星号,一行接一行。或者也可以用一小段
Python 脚本,将这个任务自动化。
bulletPointAdder.py 脚本将从剪贴板中取得文本,在每一行开始处加上星号和空
格,然后将这段新的文本贴回到剪贴板。例如,如果我将下面的文本复制到剪贴板
(取自于维基百科的文章“List of Lists of Lists”):
Lists of animals
Lists of aquarium life
Lists of biologists by author abbreviation
Lists of cultivars
然后运行 bulletPointAdder.py 程序,剪贴板中就会包含下面的内容:
- Lists of animals
- Lists of aquarium life
- Lists of biologists by author abbreviation
- Lists of cultivars
这段前面加了星号的文本,就可以粘贴回维基百科的文章中,成为一个无序列表。
从剪贴板中复制和粘贴
你希望 bulletPointAdder.py 程序完成下列事情:
1.从剪贴板粘贴文本;
2.对它做一些处理;
3.将新的文本复制到剪贴板。
代码实现:
bulletPointAdder.py `#! python3
#bulletPointAdder.py - Adds Wikipedia bullet points to the start.
of each line of the text on the clipboard.
import pyperclip
#text=‘Lists of animals\nLists of aquarium life\nLists of biologists by author abbreviation\nLists of cultivars’
text=pyperclip.paste()
#TODO:Separate lines and add stars.
#pyperclip.copy(text)
lines=text.split(’\n’)
for i in range(len(lines)):
lines[i]=’* ‘+lines[i]
text=’\n’.join(lines)
pyperclip.copy(text)
print(pyperclip.paste())`

求大神帮解!!!
本文介绍了如何使用Python脚本`bulletPointAdder.py`自动化地在维基百科文章中创建无序列表。脚本从剪贴板获取文本,为每行添加星号,然后将处理后的文本放回剪贴板。通过运行该脚本,可以高效地处理大量列表项,简化编辑过程。
1427

被折叠的 条评论
为什么被折叠?



