Unicode char replace

Discussion in 'Python' started by DiMar, Feb 12, 2008.

  1. DiMar

    DiMar Guest

    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!
     
    DiMar, Feb 12, 2008
    #1
    1. Advertising

  2. > 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-sect-23.html

    HTH,
    Martin
     
    Martin v. Löwis, Feb 12, 2008
    #2
    1. Advertising

  3. 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'»')
     
    Michael Goerz, Feb 12, 2008
    #3
  4. DiMar

    DiMar Guest

    > May I ask why?

    Of course! I have to find that string into a list of strings. This
    list includes one, using »
    Thanks! :)
     
    DiMar, Feb 12, 2008
    #4
  5. DiMar

    DiMar Guest

    On 12 Feb, 22:11, Michael Goerz <4ward.com>
    wrote:

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


    Thanks, it works!!!

    DiMar
     
    DiMar, Feb 12, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,060
    Ian Collins
    May 9, 2006
  5. Chirag Mistry
    Replies:
    6
    Views:
    172
    Ollivier Robert
    Feb 8, 2008
Loading...

Share This Page