2017-04-17 1 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.') 
관련 문제