ascii character - removing chars from string

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Jul 4 11:44:45 EDT 2006


On Tue, 04 Jul 2006 08:09:53 -0700, bruce wrote:

> simon...
> 
> the issue that i'm seeing is not a result of simply using the
> 'string.replace' function. it appears that there's something else going on
> in the text....
> 
> although i can see the nbsp in the file, the file is manipulated by a number
> of other functions prior to me writing the information out to a file.
> somewhere the 'nbsp' is changed, so there's something else going on...
> 
> however, the error i get indicates that the char 'u\xa0' is what's causing
> the issue..

As you have written it, that's not a character, it is a string of length
two. Did you perhaps mean the Unicode character u'\xa0'?

>>> len('u\xa0')
2
>>> len(u'\xa0')
1


> as far as i can determine, the string.replace can't/doesn't
> handle non-ascii chars. i'm still looking for a way to search/replace
> non-ascii chars...

Seems to work for me:

>>> c = u'\xa0'
>>> s = "hello " + c + " world"
>>> s
u'hello \xa0 world'
>>> s.replace(c, "?")
u'hello ? world'



-- 
Steven.




More information about the Python-list mailing list