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?

6
  • "tried to decode the string to utf-8"...? Commented Apr 14, 2016 at 22:51
  • like in this question stackoverflow.com/questions/13093727/… Commented Apr 14, 2016 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. Commented Apr 14, 2016 at 23:01
  • 1
    Cannot replicate with Python 3 – that is, the replacement works. Commented Apr 14, 2016 at 23:13
  • line.decode("utf-8").replace(u"\u201c", "X").encode("utf-8") AttributeError: 'str' object has no attribute 'decode' Commented Apr 14, 2016 at 23:35

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

1 Comment

That's only one, but not very generic if a string contain many unicode char

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.