473,287 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

"Disabling" raw string to print newlines

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?

I googled for an hour and couldn't find a solution.

Thank you in advance.
Jun 27 '08 #1
5 1484
ku*******@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
Jun 27 '08 #2
On May 19, 4:54*am, kuratk...@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?

I googled for an hour and couldn't find a solution.

Thank you in advance.
Change your print statement to:

print out[0]

-- Paul
Jun 27 '08 #3
On May 19, 4:54*am, kuratk...@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
Jun 27 '08 #4
On May 19, 8:09*am, Paul McGuire <pt...@austin.rr.comwrote:
On May 19, 4:54*am, kuratk...@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
Jun 27 '08 #5
On May 19, 4:01 pm, Paul McGuire <pt...@austin.rr.comwrote:
On May 19, 4:54 am, kuratk...@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
**************
This worked like a charm! :)
I used Python about a year ago and I have forgotten some of its
properties.

Thanks to both of you!
-kuratkull
>
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?
I googled for an hour and couldn't find a solution.
Thank you in advance.

Change your print statement to:

print out[0]

-- Paul
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Silmar | last post by:
Hi! In my form I have table which cells contain input objects of type="text" which initially are disabled. I would like to activate them by clicking on them. However because input object does...
3
by: Michael Conroy | last post by:
Hi... Synposis... Throws exception: "Specified argument was out of the range of valid values." Read on for the juicy tidbits. MySimpleClassCol mscc=new MySimpleClassCol(); private void...
5
by: Paul Sullivan | last post by:
We are a state agency that views protected medical information via our intranet. The screens even have privacy shields. Alarmingly, uses can "Print" and "Save As" which destroys the protection of...
22
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
44
by: Viken Karaguesian | last post by:
Hello all, On occasion I want to open hyperlinks (images, etc.) in a new window. In the past, I've used target="_blank" to open the link in a new window. However, using the "target" attribute...
12
by: Claude | last post by:
Hi, I've designed my own form checking code for name, address, email, comment box etc. What my client keeps getting in his consumer feedback forms is "spam" from companies who repeatedly...
6
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it...
3
by: Jukka K. Korpela | last post by:
Some time ago in this group, someone suggested that we should develop a "different" user style sheet to demonstrate what a user style sheet or a browser style sheet _could_ do. I guess the idea was...
3
by: Koen Vossen | last post by:
A project I'm currently working on uses Turbogears as a framework. I'd like to interact with it using the tg-admin shell. We also use gettext's _() function for internationalization. Now, this...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.