473,796 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Is there any simple way to solve this problem?

Oct 30 '06 #1
7 50092

一首诗 wrote:
Is there any simple way to solve this problem?
>>myString = " "
myString = myString.replac e(" ", "")
Oct 30 '06 #2
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?
Oct 30 '06 #3
一首诗 wrote:
Is there any simple way to solve this problem?
Yes, strings have a replace method:
>>s = "abc&nbsp;d ef"
s.replace('&n bsp;',' ')
'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;d ef"
s.replace('&nb sp;',' ')
'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(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 &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;d ef"
>>s.replace('&n bsp;',' ')
'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;d ef"
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_translatio ns = '''
"&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_Translat or = SE.SE (my_translation s)
>>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
5687
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 reduced my form request to a simple text string entry, instead of my desired optional parameters. As i have been stuck with a single unfathomable glitch for over a year. Basically, if i enter queries such as ; "select * from table" "select * from...
14
24127
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 convert n = "" # init new string for i in str: if i in chr: # if special character in str n+='\\' # append it with a backslash n+=i
13
6358
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 well as Page Valet indicate it is valid. The page is being served with the proper MIME type of "application/xhtml+xml". Unfortunately, Firefox will not display the page due to a non-breaking
12
47955
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 "&nbsp;" char - the non breaking space. Can this be represented in a grep statement at
5
11210
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 text. Its not doing what I am trying to do. The text shows up as "&ampMyValue"
1
6429
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 binding it to a repeater control. This repeater control has multiple text boxes and buttons. Can you please tell me how can i do paging in this case ? I'm posting my code below. The problem is that if i click on "AdjustThisAd" button, it opens...
0
2577
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;" instead of a blank. Never had this happen before, but I am now using Asp.Net 2.0. Ideas please?
7
4629
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 BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
5
3647
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 and rolled their own solution but I refuse to believe that Python doesn't have a built-in solution to what must be a very common problem. So, how do I convert HTML to plaintext? Something like this: <div>This&nbsp;is&nbsp;a&nbsp;string.</div>
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.