364,111 Members | 2059 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

一首诗
P: n/a
一首诗
Is there any simple way to solve this problem?

Oct 30 '06 #1
Share this Question
Share on Google+
7 Replies


martdi
P: n/a
martdi

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

wittempj@hotmail.com
P: n/a
wittempj@hotmail.com
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

Gary Herron
P: n/a
Gary Herron
一首诗 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

一首诗
P: n/a
一首诗
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

wittempj@hotmail.com
P: n/a
wittempj@hotmail.com


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

Fredrik Lundh
P: n/a
Fredrik Lundh
一首诗 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

Frederic Rentsch
P: n/a
Frederic Rentsch
一首诗 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
'''
>>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

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python