"Disabling" raw string to print newlines

Paul McGuire ptmcg at austin.rr.com
Mon May 19 09:09:06 EDT 2008


On May 19, 4:54 am, kuratk... at kuratkull.com wrote:
> Hello,
>
<snip code example scraping a QOTD from www.mcgyver.com>
>
> print out
> **************

Since you have no control over spacing and line breaks in the input,
you can reformat using the textwrap module.  First replace all "\n"s
with " ", then use re.sub to replace multiple spaces with a single
space, then call textwrap.fill to reformat the line into lines up to
'n' characters long (I chose 50 in the sample below, but you can
choose any line length you like).

out = match.findall(html)
out = out[0].replace("\n"," ")
out = re.sub("\s+"," ",out)

print textwrap.fill(out,50)


-- Paul



More information about the Python-list mailing list