473,387 Members | 1,891 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,387 software developers and data experts.

how to remove <BR> using replace function?

i have some html that looks like this
<address style="color:#">34 main,<br> Boston, MA</address>

and i am trying to use the replace function to get rid of the <Br> that
i scrape out using this code:

for oText in incident.fetchText( oRE):
strTitle += oText.strip()
strTitle = string.replace(strTitle,'<br>','')

but it doesn't seem to remove the <br>

any ideas?

Feb 9 '06 #1
10 13881
I think you want to use the replace method of the string instance.
Something like this will work:

# See http://docs.python.org/lib/string-methods.html#l2h-196
txt = "an unfortunate <br> in the middle"
txt = txt.replace("<br>", "")

Feb 9 '06 #2
tried that, didn't work for me

Feb 9 '06 #3
nope didn't work

Feb 9 '06 #4

lo************@gmail.com wrote:
nope didn't work


Could you be more specific about the error? Both my example and yours
work perfectly on my box.

Feb 9 '06 #5
Works for me.
txt = "an unfortunate <br> in the middle"
print txt.replace("<br>", "") an unfortunate in the middle

Though I don't like the 2 spaces it gives ;)

Feb 9 '06 #6
Rinzwind wrote:
Works for me.
txt = "an unfortunate <br> in the middle"
print txt.replace("<br>", "") an unfortunate in the middle

Though I don't like the 2 spaces it gives ;)

Although I generally advise against overuse of regular expressions, this is
one situation where regular expressions might be useful: the situation is
simple enough not to warrant a parser, but apart from the whitespace a <br>
tag could have attributes or be written in xhtml style <br />. Also judging
by the inconsistency between the OP's subject line and his original
question he doesn't seem sure whether the tag is <br> or <BR> or even <Br>.
import re
nobr = re.compile('\W*<br.*?>\W*', re.I)
nobr.sub(' ', "an unfortunate <br /> in the middle") 'an unfortunate in the middle' nobr.sub(' ', "an unfortunate <BR> in the middle")

'an unfortunate in the middle'
Feb 9 '06 #7


Rinzwind wrote:
Works for me.
txt = "an unfortunate <br> in the middle"
print txt.replace("<br>", "")

an unfortunate in the middle


Though I don't like the 2 spaces it gives ;)

so use regex and replace both the double spaces and the <br>

cheers
albert

Feb 9 '06 #8
lo************@gmail.com wrote:
i have some html that looks like this
<address style="color:#">34 main,<br> Boston, MA</address>

and i am trying to use the replace function to get rid of the <Br> that
i scrape out using this code:

for oText in incident.fetchText( oRE):
strTitle += oText.strip()
Why concatening ?
strTitle = string.replace(strTitle,'<br>','')
Use strTitle.replace('<br>', '') instead. And BTW, hungarian notation is
evil, so:
for text in incident.fetchText(...):
title = text.strip().replace('<br>', '')
but it doesn't seem to remove the <br>
it does :

Python 2.4.2 (#1, Feb 9 2006, 02:40:32)
[GCC 3.4.5 (Gentoo 3.4.5, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
s = '<address style="color:#">34 main,<br> Boston, MA</address>'
s.replace('<br>', '') '<address style="color:#">34 main, Boston, MA</address>'


The problem is obviously not with str.replace(), as you could have
figured out by yourself very easily.
any ideas?


yes: post the minimal *running* code that exhibit the problem.

Your problem is probably elsewhere, and given some of previous posts
here ('problems writing tuple to log file' and 'indentation messing up
my tuple?'), I'd say that a programming101 course should be your first
move.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Feb 9 '06 #9
Duncan Booth <du**********@suttoncourtenay.org.uk> wrote:
Although I generally advise against overuse of regular expressions, this is
one situation where regular expressions might be useful: [ ... ]
nobr = re.compile('\W*<br.*?>\W*', re.I)


Agreed (on both counts), but r'\s*<br.*?>\s*' might be better
(consider what happens with "an unfortunate... <br> in the middle"
if you use \W rather than \s).

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Feb 9 '06 #10
Sion Arrowsmith wrote:
Duncan Booth <du**********@suttoncourtenay.org.uk> wrote:
Although I generally advise against overuse of regular expressions,
this is one situation where regular expressions might be useful: [ ...
]
> nobr = re.compile('\W*<br.*?>\W*', re.I)


Agreed (on both counts), but r'\s*<br.*?>\s*' might be better
(consider what happens with "an unfortunate... <br> in the middle"
if you use \W rather than \s).


Yes, I don't really know why I wrote \W when I obviously meant \s. Thanks
for correcting that.

Even better might be r'(\s*<br.*?>)+\s*' to get multiple runs of <br> tags.
Feb 10 '06 #11

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

Similar topics

4
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
6
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
7
by: noor.rahman | last post by:
I have an XML file that stores data from an HTML form. I use XSL to display the data in HTML format. The data may have newline characters. However, XSL is not displaying the newlines properly in...
1
by: Rolan | last post by:
I need to be able to remove <BR>'s that are placed in null fields (Access 97) for blank records when doing html imports. Of course, Access does not recognize the html tags and are invisible. Aside...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
2
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
1
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
13
by: CK | last post by:
Hi all, I have a textarea control. I am putting it's value in an html email. The problem is that the new lines are being ignored. I want to take the controls value and replace any newline...
2
by: xhe | last post by:
I met a very headache problem in javascript, I think this might be difference between IE and NS / Safari. I have a text area <form> <textarea name='tex1' onkeyup='displayit();'></textarea>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.