2017-04-17 74 views
1

我在MacOS Sierra上使用Python 3.5。我正在研究這個課程,用Python自動化枯燥的東西,並且遇到了pyperclip的問題。下面的代碼適用於僅複製pdf的4行時,但是當我複製所有文本時,我會收到一條錯誤消息(如下所示)。pyperclip不適用於大型文本

有人可以幫助我嗎?這是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 

這裏是我的code

#! 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''' 
# [email protected](\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) 

回答

0

你可以試着改變進口:進口pyperclip,再?

此外,這只是我的代碼的一個例子,如果它有幫助,因爲我使用的是同一本書。

#! python3 
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard. 

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) 

# Create email regex. 
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) 



# Find matches in clipboard text. 
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]) 


# 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.')