Reputation: 1709
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
Upvotes: 0
Views: 162
Reputation: 149075
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'))
Upvotes: 2
Reputation: 6189
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
Upvotes: 1