Not sure if this is sufficient for what you need, but how about<br><br>import re<br>re.sub(u'[\s\xa0]+', ' ', s)<br><br>That should replace all occurances of 1 or more whitespace or \xa0 characters, by a single space.
<br><br>Remco<br><br><div class="gmail_quote">On Jan 19, 2008 12:38 PM, John Machin <<a href="mailto:sjmachin@lexicon.net">sjmachin@lexicon.net</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm trying to recover the original data from some HTML written by a<br>well-known application.<br><br>Here are three original data items, in Python repr() format, with<br>spaces changed to tildes for clarity:<br><br>u'Saturday,~19~January~2008'
<br>u'Line1\nLine2\nLine3'<br>u'foonly~frabjous\xa0farnarklingliness'<br><br>Here is the HTML, with spaces changed to tildes, angle brackets<br>changed to square brackets,<br>omitting \r\n from the end of each line, and stripping a large number
<br>of attributes from the [td] tags.<br><br>~~[td]Saturday,~19<br>~~January~2008[/td]<br>~~[td]Line1[br]<br>~~~~Line2[br]<br>~~~~Line3[/td]<br>~~[td]foonly<br>~~frabjous&nbsp;farnarklingliness[/td]<br><br>Here are the results of feeding it to ElementSoup:
<br><br>>>> import ElementSoup as ES<br>>>> elem = ES.parse('ws_soup1.htm')<br>>>> from pprint import pprint as pp<br>>>> pp([(e.tag, e.text, e.tail) for e in elem.getiterator()])
<br>[snip]<br> (u'td', u'Saturday, 19\n  January 2008', u'\n'),<br> (u'td', u'Line1', u'\n'),<br> (u'br', None, u'\n    Line2'),<br> (u'br', None, u'\n    Line3'),
<br> (u'td', u'foonly\n  frabjous\xa0farnarklingliness', u'\n')]<br><br>I'm happy enough with reassembling the second item. The problem is in<br>reliably and<br>correctly collapsing the whitespace in each of the above five
<br>elements. The standard Python<br>idiom of u' '.join(text.split()) won't work because the text is<br>Unicode and u'\xa0' is whitespace<br>and would be converted to a space.<br><br>Should whitespace collapsing be done earlier? Note that BeautifulSoup
<br>leaves it as &nbsp; -- ES does the conversion to \xa0 ...<br><br>Does anyone know of an html_collapse_whitespace() for Python? Am I<br>missing something obvious?<br><br>Thanks in advance,<br>John<br><font color="#888888">
--<br><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br></font></blockquote></div><br>