Is there any simple way to solve this problem?
How to convert " " in a string to blank space?
Collapse
This topic is closed.
X
X
-
一首诗Tags: None -
martdi
Re: How to convert "&nbsp ;" in a string to blank space?
一首诗 wrote:Is there any simple way to solve this problem?>>myString = " "
>>myString = myString.replac e(" ", "") -
wittempj@hotmail.com
Re: How to convert "&nbsp ;" in a string to blank space?
Is this what you want?
pys = 'This string contains two times - end'
pyprint s.replace('&nbs p;', ' '*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?Comment
-
Gary Herron
Re: How to convert "&nbsp ;" in a string to blank space?
一首诗 wrote:Yes, strings have a replace method:Is there any simple way to solve this problem?
>
'abc def'>>s = "abc d ef"
>>s.replace('&n bsp;',' ')
Also various modules that are meant to deal with web and xml and such
have functions to do such operations.
Gary Herron
Comment
-
一首诗
Re: How to convert "&nbsp ;" in a string to blank space?
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 " " to blank space and convert <brto "\r\n"
Gary Herron wrote:一首诗 wrote:Yes, strings have a replace method:Is there any simple way to solve this problem?
>'abc def'>s = "abc d ef"
>s.replace('&nb sp;',' ')
>
Also various modules that are meant to deal with web and xml and such
have functions to do such operations.
Gary HerronComment
-
wittempj@hotmail.com
Re: How to convert "&nbsp ;" in a string to blank space?
On Oct 30, 6:44 pm, "一首诗" <newpt...@gmail .comwrote:Then you can explore the parser,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 " " to blank space and convert <brto "\r\n"
>
http://docs.python.org/lib/module-HTMLParser.html, like
#!/usr/bin/env python
from HTMLParser import HTMLParser
parsedtext = ''
class Parser(HTMLPars er):
def handle_starttag (self, tag, attrs):
if tag == 'br':
global parsedtext
parsedtext += '\\r\\n'
def handle_data(sel f, data):
global parsedtext
parsedtext += data
def handle_entityre f(self, name):
if name == 'nbsp':
pass
x = Parser()
x.feed('An text<br>')
print parsedtext
Gary Herron wrote:>一首诗 wrote:Is there any simple way to solve this problem?>Yes, strings have a replace method:>'abc def'>>s = "abc d ef"
>>s.replace('&n bsp;',' ')Also various modules that are meant to deal with web and xml and such
have functions to do such operations.Gary HerronComment
-
Fredrik Lundh
Re: How to convert "&nbsp ;" in a string to blank space?
一首诗 wrote:
corresponds to a non-breaking space, chr(160). if you're onlyIs there any simple way to solve this problem?
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:
</F>
Comment
-
Frederic Rentsch
Re: How to convert "&nbsp ;" in a string to blank space?
一首诗 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 " " to blank space and convert <brto "\r\n"
>
Gary Herron wrote:
>>>一首诗 wrote:
>>>Yes, strings have a replace method:>>Is there any simple way to solve this problem?
>>>
>>>
>>>
>>
>>>'abc def'>>>>s = "abc d ef"
>>>>s.replace(' ',' ')
>>>>>
>>
>Also various modules that are meant to deal with web and xml and such
>have functions to do such operations.
>>
>>
>Gary Herron
>>
>>>my_translatio ns = '''
# "<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_Translat or = SE.SE (my_translation s)ABC DEFG>>print My_Translator ('ABC DEFG <br>XYZ')
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
Comment
Comment