Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
Currently I am going through the automate the boring stuff with python video course and am basically copying his shown code and trying to create the program that way, my code is currently the exact same as in the video.
1st I will state the function of the program intended than 2nd why I think it might be having the error stated above and would appreciate someone helping the noob out on solving this.
The program is intended take a pdf document that we have copied to the clipboard, the document contains emails and phone numbers. We want it to extract the email and phone and than copy that info at the end to the clipboard.
What is different between the instructor and I is I am using a different document to copy and extract phone numbers as the one he used no longer exists. The current output if I print the phone numbers copied is the letter u and than the intended phone number
Current program output
import re, pyperclip
# Create a regex for phone numbers
phoneRegex = re.compile(r'''
(((\d\d\d)|(\(\d\d\d\)))? # area code <optional>
(\s|-) # first seperator
\d\d\d # first 3 digitis
- # seperator
\d\d\d\d # last 4 digits
(((ext(\.)?\s)|x) # extension word-part<optional>
(\d{2,5}))? # extension number-part<optional> 2,5 is to signify that it can be 2-5 digits
''', re.VERBOSE)
# TODO:: Create a regex object for email addresses
emailRegex = re.compile(r'''
# we will make it search for emails that contain any numbers, letters plus or period symbols
[a-zA-Z0-9_.+]+ # name part
@ # @ symbol
[a-zA-Z0-9_.+]+ # domain part
''', re.VERBOSE)
# Get the text off the clipboard
text = pyperclip.paste()
# Extract the email/phone from this text
extractedPhone = phoneRegex.findall(text)
extractedEmail = emailRegex.findall(text)
allPhoneNumbers = []
for phoneNumber in extractedPhone:
allPhoneNumbers.append(phoneNumber[0])
print(allPhoneNumbers)
# TODO: Copy the extracted email/phone to the cliboard
results = '\n'.join(allPhoneNumbers) + '\n' + '\n'.join(extractedEmail)
pyperclip.copy(results)
You can try to make allPhoneNumbers list variable as string variable and instead of appending it just concatenate the string, because pyperclip.paste() or pyperclip.copy() function is expecting string as argument.
Here you are trying to insert a list into the paste() function that is the reason even i was getting the same error then just convert the list to string then will work perfectly.
And one more important thing findAll() function always return a tuple so convert it also in string before concatenating to the resultant string.
I just had the same issue and I see it is probably a regression in pyperclip.
Try installing an older version.
This worked for me.
python2 -m pip install 'pyperclip<1.6.2'
or simply
pip install 'pyperclip<1.6.2'
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.