当前位置:嗨网首页>书籍在线阅读

31-第4步_将所有匹配连接成一个字符串,复制到剪贴板

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

第4步:将所有匹配连接成一个字符串,复制到剪贴板

现在, E-mail 地址和电话号码已经作为字符串列表放在 matches 中,你希望将它们复制到剪贴板。 pyperclip.copy() 函数只接收一个字符串值,而不是字符串的列表,因此需要在 matches 上调用 join() 方法。

为了更容易看到程序在工作,让我们将所有找到的匹配都输出在命令行窗口上。如果没有找到电话号码或 E-mail 地址,程序应该发出信息告诉用户。

让你的程序看起来像这样:

#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
--snip--
for groups in emailRegex.findall(text):
 matches.append(groups[0])
# Copy results to the clipboard.
if len(matches) > 0:
 pyperclip.copy('\n'.join(matches))
 print('Copied to clipboard:')
 print('\n'.join(matches))
else:
 print('No phone numbers or email addresses found.')