[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Unicode char replace

DiMar

2/12/2008 8:54:00 PM

Hi all,

I have this unicode string:

string = u'Macworld » Jobs 1 - Twitter 0'

and I want to replace the '»' (aka \xbb) char to '&raquo'.
I've tried 2 ways:

1.
>>> string2 = string.replace('\\xbb','»')
u'Macworld \xbb Jobs 1 - Twitter 0'

2.
>>> import cgi
>>> string2 = cgi.escape(string).encode("ascii", "xmlcharrefreplace")
>>> string2
'Macworld » Jobs 1 - Twitter 0'

None of them gives me 'Macworld » Jobs 1 - Twitter 0'

Any idea?

Thanks!
4 Answers

Martin v. Loewis

2/12/2008 9:02:00 PM

0

> string = u'Macworld » Jobs 1 - Twitter 0'
>
>
> None of them gives me 'Macworld » Jobs 1 - Twitter 0'
>
> Any idea?

So I assume you *want* it to produce ». May I ask why?
I really recommend that you use » instead.

In any case, you need to define your own error handler, such as
the one in

http://herlock.com/ob/pythoncb/0596007973/chp-1-se...

HTH,
Martin

Michael Goerz

2/12/2008 9:12:00 PM

0

DiMar wrote, on 02/12/2008 09:54 PM:
> Hi all,
>
> I have this unicode string:
>
> string = u'Macworld » Jobs 1 - Twitter 0'
>
> and I want to replace the '»' (aka \xbb) char to '&raquo'.
> I've tried 2 ways:
>
> 1.
>>>> string2 = string.replace('\\xbb','»')
> u'Macworld \xbb Jobs 1 - Twitter 0'
How about this?
string.replace(u'\xbb', u'»')

DiMar

2/12/2008 9:15:00 PM

0

> May I ask why?

Of course! I have to find that string into a list of strings. This
list includes one, using »
Thanks! :)

DiMar

2/12/2008 9:16:00 PM

0

On 12 Feb, 22:11, Michael Goerz <newsgroup898s...@8439.e4ward.com>
wrote:

> How about this?
> string.replace(u'\xbb', u'&raquo;')

Thanks, it works!!!

DiMar