1

I have a string where some of the characters appear as unicode, e.g.: "bla bla bla \uf604 bla bla bla"

I tried doing string = string.replace("\uf604", "X"), but nothing happens. I tried to decode the string to utf-8, but apparently that doesn't work in Python 3.

How can I replace the character?

user2817012
  • 261
  • 2
  • 11
  • "tried to decode the string to utf-8"...? – Ilja Everilä Apr 14 '16 at 22:51
  • like in this question http://stackoverflow.com/questions/13093727/how-to-replace-unicode-characters-in-string-with-something-else-python – user2817012 Apr 14 '16 at 22:57
  • Ah, from utf-8. There's a lot of confusion about encoding unicode/decoding bytes stemming from Py2. Could you add to your question a bit more details about what and how you've tried? If you get tracebacks, add them too. – Ilja Everilä Apr 14 '16 at 23:01
  • 1
    Cannot replicate with Python 3 – that is, the replacement works. – Jongware Apr 14 '16 at 23:13
  • line.decode("utf-8").replace(u"\u201c", "X").encode("utf-8") AttributeError: 'str' object has no attribute 'decode' – user2817012 Apr 14 '16 at 23:35
  • how do you display the input string? Is there a literal backslash in it or is it just a Unicode escape sequence that is used in the text *representation* of the string (only printable characters represent themselves, non-printable characters are escaped). Note: `'A' == '\u0041' != r'\u0041'` – jfs Apr 15 '16 at 05:51

1 Answers1

7

In Python 3, this works (although the print may not, depending on your terminal):

>>> s="bla bla bla \uf604 bla bla bla"
>>> print(s)
bla bla bla  bla bla bla
>>> s="bla bla bla \uf604 bla bla bla"
>>> s.replace('\uf604','X')
'bla bla bla X bla bla bla'

But perhaps you have a literal slash and not an escape code. Note the print difference:

>>> s="bla bla bla \\uf604 bla bla bla"
>>> print(s)
bla bla bla \uf604 bla bla bla
>>> s.replace('\uf604','X')
'bla bla bla \\uf604 bla bla bla'

Use a escape slash to fix:

>>> s.replace('\\uf604','X')
'bla bla bla X bla bla bla'
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251