我目前正在阅读一本自学 Python 的书,我遇到了一个我似乎无法得到好的答案的错误。错误是:
'File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyperclip/__init__.py", line 596, in lazy_load_stub_paste
return paste()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyperclip/__init__.py", line 109, in paste_osx_pbcopy
return stdout.decode(ENCODING)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 197: invalid start byte'
有问题的代码行:
text = str(pyperclip.paste())
完整脚本:
import pyperclip, re
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
(\d{3}) # first 3 digits
(\s|-|\.)? # separator
(\d{4}) # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? #extension
)''', re.VERBOSE)
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # username
@ # @ symbol
[a-zA-Z0-9.-]+ # domain name
(\.[a-zA-Z]{2,4}) # dot-something
)''', re.VERBOSE)
text = str(pyperclip.paste())
matches = []
for groups in phoneRegex.findall(text):
phoneNum = '-'.join([groups[1], groups[3], groups[5]])
if groups[8] != '':
phoneNum += ' x' +groups[8]
matches.append(phoneNum)
for groups in emailRegex.findall(text):
matches.append(groups[0])
if len(matches) > 0:
pyperclip.copy('\n'.join(matches))
print('Copied to clipboard:')
print('\n'.join(matches))
else:
print('No phone numbers of email addresses found.')
让我知道我是否可以提供其他任何东西来解决这个问题。从我的搜索来看,这似乎是一个编码错误,但我一点也不知道如何解决它。
我很感激任何反馈。
谢谢你。