@zhangyu756897669
2017-09-02T09:15:31.000000Z
字数 3431
阅读 531
您可能在许多不同的网站上有帐户。对于每一个使用相同的密码来说,这是一个不好的习惯,因为如果这些网站有任何安全漏洞,黑客将会向所有其他帐户使用密码。最好在计算机上使用密码管理器软件,使用一个主密码来解锁密码管理器。然后,您可以将任何帐户密码复制到剪贴板,并将其粘贴到网站的密码字段中。
第1步:程序设计和数据结构
您希望能够使用帐户名称的命令行参数(例如电子邮件或博客)来运行此程序。该帐户的密码将被复制到剪贴板,以便用户将其粘贴到密码字段中。这样,用户可以使用长而复杂的密码,而无需记住它们。
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
'luggage': '12345'}
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
'luggage': '12345'}
import sys
if len(sys.argv) < 2:
print('Usage: python pw.py [account] - copy account password')
sys.exit()
account = sys.argv[1] # first command line arg is the account name
#! python3
# pw.py - An insecure password locker program.
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
'luggage': '12345'}
import sys, pyperclip
if len(sys.argv) < 2:
print('Usage: py pw.py [account] - copy account password')
sys.exit()
account = sys.argv[1] # first command line arg is the account name
if account in PASSWORDS:
pyperclip.copy(PASSWORDS[account])
print('Password for ' + account + ' copied to clipboard.')
else:
print('There is no account named ' + account)
该新代码在PASSWORDS字典中查找帐户名称。如果帐户名称是字典中的一个键,我们将获得与该键对应的值,将其复制到剪贴板,并打印一条消息,表示我们复制了该值。否则,我们打印一个消息,说没有帐号与该名称。
编辑维基百科文章时,您可以通过将每个列表项目放在自己的行上并在前面放置一个*来创建一个项目符号列表。但是说你有一个很大的列表,你想添加一些点。你可以直接在每一行的开头逐个输入这些星号。或者您可以使用一个简短的Python脚本自动执行此任务。
bulletPointAdder.py脚本将从剪贴板中获取文本,并将每个行的开头添加星号和空格,然后将此新文本粘贴到剪贴板。例如,如果我将以下文本(维基百科文章“列表列表”)复制到剪贴板:
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. 将新文本复制到剪贴板
#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.
import pyperclip
text = pyperclip.paste()
# TODO: Separate lines and add stars.
pyperclip.copy(text)
'Lists of animals\nLists of aquarium life\nLists of biologists by author
abbreviation\nLists of cultivars'
此字符串中的\ n换行符会在从剪贴板打印或粘贴时显示多行。这一个字符串值中有很多“行”。你想要添加一个明星到每一行的开头。
您可以编写搜索字符串中每个\ n换行符的代码,然后在之后添加星号。但是,使用split()方法可以更容易地返回一个字符串列表,一个用于原始字符串中的每一行,然后将星号添加到列表中每个字符串的前面。
#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.
import pyperclip
text = pyperclip.paste()
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes in the "lines" list
lines[i] = '* ' + lines[i] # add star to each string in "lines" list
pyperclip.copy(text)
#! python3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.
import pyperclip
text = pyperclip.paste()
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes for "lines" list
lines[i] = '* ' + lines[i] # add star to each string in "lines" list
text = '\n'.join(lines)
pyperclip.copy(text)