我在 MacOS Sierra 上使用 Python 3.5。我正在学习这门课程,用 Python 自动化无聊的东西,并且遇到了 pyperclip 的问题。当我只复制 4 行pdf时,代码(下)有效,但是当我复制所有文本时,我收到一条错误消息(下)。
有人可以帮助我吗?pyperclip有问题吗?我的代码?我的电脑?
错误信息:
Traceback (most recent call last):
File "/Users/ericgolden/Documents/MyPythonScripts/phoneAndEmail.py", line 35, in <module>
text = pyperclip.paste()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyperclip/clipboards.py", line 22, in paste_osx
return stdout.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 79: invalid continuation byte
这是我的代码:
#! python3
import re, pyperclip
# Create a regex for phone numbers
phoneRegex = re.compile(r'''
# 415-555-000, 555-0000, (415) 555-0000, 555-000 ext 12345, ext. 12345, x12345
(
((\d\d\d) | (\(\d\d\d\)))? # area code optional
(\s|-) # first separator
\d\d\d # first 3 digits
- # seperator
\d\d\d\d # last 4 digits
(((ext(\.)?\s)|x) # extension word-part optional
(\d{2,5}))? # extension number-part optional
)
''', re.VERBOSE)
# Create a regex for email addresses
emailRegex = re.compile(r'''
# some.+_things@(\d{2,5}))?.com
[a-zA-Z0-9_.+]+ # name part
@ # @ symbol
[a-zA-Z0-9_.+]+ # domain name part
''', re.VERBOSE)
# Get the text off the clipboard
text = pyperclip.paste()
# TODO: Extract the email/phone from this text
extractedPhone = phoneRegex.findall(text)
extractedEmail = emailRegex.findall(text)
allPhoneNumbers = []
for phoneNumber in extractedPhone:
allPhoneNumbers.append(phoneNumber[0])
# TODO: Copy the extraced email/phone to the clipboard
results = '\n'.join(allPhoneNumbers) + '\n' + '\n'.join(extractedEmail)
pyperclip.copy(results)