简体   繁体   中英

Replacing a unicode character in a string in Python 3

I have a string where some of the characters appear as unicode, eg: "bla bla bla \ bla bla bla"

I tried doing string = string.replace("\", "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?

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'

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM