Python’s join method combines the items in an iterable into one string, placing the receiving string between adjacent items. It is useful for rebuilding cleaned email text while preserving spaces, symbols, or line breaks. section 9.3.2
Joining cleaned email lines
Given lines such as ['first line', 'second', 'the last line'], ''.join(lines) squishes the words together, ' '.join(lines) separates them with spaces, '*'.join(lines) inserts asterisks, and '\n'.join(lines) preserves separate lines. In an email-cleanup tool, text = '\n'.join(lines) rebuilds the cleaned message before copying it to the clipboard. section 9.3.2
Listing 9.1 First try: Clean up email text in the clipboard
import pyperclip
def clean_email():
'''
The clipboard contains lines of text.
Clean up the text by removing any > or space
characters from the beginning of each line.
Replace the clipboard with the cleaned text.
'''
text = pyperclip.paste() #1
lines = text.splitlines() #2
for i in range(len(lines)): #3
lines[i] = lines[i].lstrip(' >') #4
text = ' #5
'.join(lines) #5
pyperclip.copy(text) #6
if __name__ == '__main__': #7
clean_email() #8
View the complete clipboard-cleanup listing, including splitlines, line cleanup, join, and clipboard replacement. section 9.3.2





