"Disabling" raw string to print newlines
Paul McGuire
ptmcg at austin.rr.com
Mon May 19 09:18:53 EDT 2008
On May 19, 8:09 am, Paul McGuire <pt... at austin.rr.com> wrote:
> On May 19, 4:54 am, kuratk... at kuratkull.com wrote:> Hello,
>
> <snip code example scraping a QOTD fromwww.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
One last try - .replace("\n"," ") is unnecessary, textwrap.fill takes
care of removing extra newlines already.
out = match.findall(html)
out = out[0]
out = re.sub("\s+"," ",out)
print textwrap.fill(out,50)
-- Paul
More information about the Python-list
mailing list