这是编写在剪贴板中搜索某些单词的脚本的正确方法吗?

时间:2019-05-12 20:43:11

标签: python python-3.x

我是python的初学者,我试图编写一个程序以在剪贴板中搜索某些单词(作为参数)。

我写了一些代码,它似乎可以工作。但是我想知道一些经验丰富的程序员如何编写这种程序,以及是否犯了一些错误。

#! python3
# searcher.py - Searches in the clipboard the occurence of some words specified as arguments.
# Usage: py.exe searcher.py <word1> <word2>...

import re, sys, pyperclip


#Make sure there is at least one argument.
if len(sys.argv) == 1:
    print('You have to enter a word as an argument')

#Find the occurences in clipboard text.
text = pyperclip.paste()
matches = []
for i in range(1, len(sys.argv)):
    word = sys.argv[i]         
    text_regex = re.compile(r'\b({0})\b'.format(word), flags=re.IGNORECASE)
    for occurences in text_regex.findall(text):
        matches.append(occurences)

# 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 occurences found')

感谢您的帮助:)

0 个答案:

没有答案