[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

return [(line + '\n') for line in lines] # put them back


def wrapLines2(lines, size=defaultsize): # more uniform across lines
"same, but concat as one long line" # but loses original structure
text = ''.join(lines) # put together as 1 line
lines = wrapText2(text) # wrap on delimiters
return [(line + '\n') for line in lines] # put newlines on ends


###############################################################################


self-test


###############################################################################


if name == 'main':
lines = ['spam ham ' 20 + 'spam,ni' 20,
'spam ham ' 20,
'spam,ni'
20,
'spam ham.ni' 20,
'',
'spam'
80,
' ',
'spam ham eggs']


sep = '-' * 30
print('all', sep)
for line in lines: print(repr(line))
print('simple', sep)
for line in wrapLinesSimple(lines): print(repr(line))
print('smart', sep)
for line in wrapLinesSmart(lines): print(repr(line))


print('single1', sep)
for line in wrapLinesSimple([lines[0]], 60): print(repr(line))
print('single2', sep)
for line in wrapLinesSmart([lines[0]], 60): print(repr(line))
print('combined text', sep)
for line in wrapLines2(lines): print(repr(line))
print('combined lines', sep)
print(wrapText1('\n'.join(lines)))


assert ''.join(lines) == ''.join(wrapLinesSimple(lines, 60))
assert ''.join(lines) == ''.join(wrapLinesSmart(lines, 60))
print(len(''.join(lines)), end=' ')
print(len(''.join(wrapLinesSimple(lines))), end=' ')
print(len(''.join(wrapLinesSmart(lines))), end=' ')
print(len(''.join(wrapLinesSmart(lines, 60))), end=' ')
input('Press enter') # pause if clicked


html2text: Extracting Text from HTML (Prototype, Preview)


Example 14-8 lists the code of the simple-minded HTML parser that PyMailGUI uses
to extract plain text from mails whose main (or only) text part is in HTML form. This
extracted text is used both for display and for the initial text in replies and forwards.


1102 | Chapter 14: The PyMailGUI Client

Free download pdf