Replacing a unicode character in a string in Pytho

2019-07-25 07:18发布

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?

2条回答
Melony?
2楼-- · 2019-07-25 07:46

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'
查看更多
Viruses.
3楼-- · 2019-07-25 07:55

You can use the replace-method if you tell python to use a raw string:

s = r"bla bla bla \uf604 bla bla bla"
s = s.replace(r"\uf604", "X")

results in s ='bla bla bla X bla bla bla'

查看更多
登录 后发表回答