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

G

Gary Herron

一首诗 said:
Is there any simple way to solve this problem?
Yes, strings have a replace method:
'abc def'

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


Gary Herron
 
G

Guest

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 <br> to "\r\n"
 
F

Fredrik Lundh

一首诗 said:
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>
 
W

wittempj

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 <br> to "\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
 
F

Frederic Rentsch

一首诗 said:
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 <br> to "\r\n"
"&nbsp;= "
# "<br>=\r\n" "<BR>=\r\n" # Windows
"<br>=\n" "<BR>=\n" # Linux
# Add others to your heart's content
'''
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top