"Disabling" raw string to print newlines
Diez B. Roggisch
deets at nospam.web.de
Mon May 19 06:01:56 EDT 2008
kuratkull at kuratkull.com wrote:
> Hello,
>
> ***************
> import urllib2
> import re
> import string
> import sys
>
> url = "http://www.macgyver.com/"
> request = urllib2.Request(url)
> opener = urllib2.build_opener()
> html = opener.open(request).read()
>
> match = re.compile("<PRE>(.+)</PRE>", re.DOTALL)
>
> out = match.findall(html)
>
> print out
> **************
>
> I would like to print out string with formatting, but as I read, the
> string is made into a raw string when using re.
> How could I disable or bypass this?
You have a misconception here. A raw-string in python is *only* different as
literal - that is, you can write
r"fooo\bar"
where you'd have to write
"fooo\\bar"
with "normal" string-literals. However, the result of both is a byte-string
object that is exactly equal. So whatever out contains, it has nothing to
do with raw-string or not.
But what you probably mean is that putting out a list using print will use
the repr()-call on the contained objects. So instead of doing
print out
do
print "\n".join(out)
or such.
Diez
More information about the Python-list
mailing list