473,324 Members | 2,417 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,324 software developers and data experts.

How to convert " " in a string to blank space?

Is there any simple way to solve this problem?

Oct 30 '06 #1
7 49471

一首诗 wrote:
Is there any simple way to solve this problem?
>>myString = " "
myString = myString.replace(" ", "")
Oct 30 '06 #2
Is this what you want?

pys = 'This string contains   two times   - end'
pyprint s.replace(' ', ' '*6)
This string contains two times - end
see http://docs.python.org/lib/string-methods.html

On Oct 30, 6:26 pm, "一首诗" <newpt...@gmail.comwrote:
Is there any simple way to solve this problem?
Oct 30 '06 #3
一首诗 wrote:
Is there any simple way to solve this problem?
Yes, strings have a replace method:
>>s = "abc&nbsp;def"
s.replace('&nbsp;',' ')
'abc def'

Also various modules that are meant to deal with web and xml and such
have functions to do such operations.
Gary Herron

Oct 30 '06 #4
Oh, I didn't make myself clear.

What I mean is how to convert a piece of html to plain text bu keep as
much format as possible.

Such as convert "&nbsp;" to blank space and convert <brto "\r\n"

Gary Herron wrote:
一首诗 wrote:
Is there any simple way to solve this problem?
Yes, strings have a replace method:
>s = "abc&nbsp;def"
s.replace('&nbsp;',' ')
'abc def'

Also various modules that are meant to deal with web and xml and such
have functions to do such operations.
Gary Herron
Oct 30 '06 #5


On Oct 30, 6:44 pm, "一首诗" <newpt...@gmail.comwrote:
Oh, I didn't make myself clear.

What I mean is how to convert a piece of html to plain text bu keep as
much format as possible.

Such as convert "&nbsp;" to blank space and convert <brto "\r\n"
Then you can explore the parser,
http://docs.python.org/lib/module-HTMLParser.html, like

#!/usr/bin/env python
from HTMLParser import HTMLParser

parsedtext = ''

class Parser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag == 'br':
global parsedtext
parsedtext += '\\r\\n'

def handle_data(self, data):
global parsedtext
parsedtext += data

def handle_entityref(self, name):
if name == 'nbsp':
pass

x = Parser()
x.feed('An &nbsp; text<br>')
print parsedtext

Gary Herron wrote:
一首诗 wrote:
Is there any simple way to solve this problem?
Yes, strings have a replace method:
>>s = "abc&nbsp;def"
>>s.replace('&nbsp;',' ')
'abc def'
Also various modules that are meant to deal with web and xml and such
have functions to do such operations.
Gary Herron
Oct 30 '06 #6
一首诗 wrote:
Is there any simple way to solve this problem?
&nbsp; corresponds to a non-breaking space, chr(160). if you're only
dealing with this specific XML/HTML entity, you can do

text = text.replace("&nbsp;", " ")

or

text = text.replace("&nbsp;", chr(160))

to handle arbitrary entities and character references, pass the data
through an HTML or XML parser, or use something like:

http://effbot.org/zone/re-sub.htm#unescape-html

</F>

Oct 30 '06 #7
一首诗 wrote:
Oh, I didn't make myself clear.

What I mean is how to convert a piece of html to plain text bu keep as
much format as possible.

Such as convert "&nbsp;" to blank space and convert <brto "\r\n"

Gary Herron wrote:
>一首诗 wrote:
>>Is there any simple way to solve this problem?
Yes, strings have a replace method:

>>>>s = "abc&nbsp;def"
s.replace('&nbsp;',' ')
>
'abc def'

Also various modules that are meant to deal with web and xml and such
have functions to do such operations.
Gary Herron

>>my_translations = '''
"&nbsp;= "
# "<br>=\r\n" "<BR>=\r\n" # Windows
"<br>=\n" "<BR>=\n" # Linux
# Add others to your heart's content
'''
>>import SE # From http://cheeseshop.python.org/pypi/SE/2.2%20beta
>>My_Translator = SE.SE (my_translations)
>>print My_Translator ('ABC&nbsp;DEFG<br>XYZ')
ABC DEFG
XYZ

SE can also strip tags and translate all HTM escapes and generally lets
you do ad hoc translations in seconds. You just write them up, make an
SE object from your text an run your data through it. As simple as that.
If you wish further explanations, I'll be happy to explain.

Frederic
Oct 30 '06 #8

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

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
13
by: rbronson1976 | last post by:
Hi all, I have a very simple page that Firefox has problems with: www.absolutejava.com/testing.htm First of all, this page seems to be perfectly valid XHTML Strict. Both the W3C validator as...
12
by: Robert Mark Bram | last post by:
Hi All, I am using the following trim function: function trim (str) { return str.replace(/^\s*/g, '').replace(/\s*$/g, ''); } The problem is that this doesn't trim instances of the...
5
by: Naveen K Kohli | last post by:
I am try to set the text of the drop down list item as ListItem li = new ListItem(); li.Text = "&nbsp;"+"MyValue"; myDropDown.Items.Add("li); The intent is to add a spacing in front of the...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
0
by: K B | last post by:
Hi again, I have a gridview, when I get the selecteditem.cells for a column, if the database column is Null or Empty, and I assign that to my web form text control, the control reads "&nbsp;"...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
5
by: Just Another Victim of the Ambient Morality | last post by:
I've done a google search on this but, amazingly, I'm the first guy to ever need this! Everyone else seems to need the reverse of this. Actually, I did find some people who complained about this...
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
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.