简体   繁体   中英

Error while try to remove Unicode special character in python 2.7 TypeError: expected a string or other character buffer object

I try to remove Unicode special character in python 2.7 but i got error

newr = r.translate ({ord(c): "" for c in "“”"}) TypeError: expected a string or other character buffer object

here my code :

# -*- coding: utf-8 -*-
r= "“សួស្តី” – “អ្នក” – “Ok”"
newr = r.translate ({ord(c): "" for c in "“”"})
print (newr)

and i want remove this.

“ ”

PS: this is Khmer Unicode

In Python 2.7, the str type is a byte string and has no notion of unicode characters. Simply some byte sequence may happen to be utf-8 encoded unicode characters. If you want to process unicode characters, you must decode the byte string into a unicode one with ur = r.decode('utf-8') .

Moreover, the translate method has different syntax when called on a byte string and on a unicode one. Your code use the unicode syntax but is applied to a byte string, hence the error.

What is want is:

# -*- coding: utf-8 -*-
r= "“សួស្តី” – “អ្នក” – “Ok”"

newr = r.decode('utf-8').translate ({ord(c): None for c in u"“”"})
print (newr)      # or print(newr.encode('utf-8'))

Try

r = "“សួស្តី” – “អ្នក” – “Ok”"
newr = r.replace("“","").replace("”","")
print (newr)

or

r = "“សួស្តី” – “អ្នក” – “Ok”"
newr = ''.join(x for x in r if x != '”' and x != '“')
print (newr)

Edit: you edited your question so my answer might not be valid anymore, but it still seems to work for me

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