Drexler Signe
Drexler Signe

Reputation: 1

Replacing a string from an built-in unknown character in python

I'm trying to replace this unrecognized � text below here to a another text. What I did is decode it to UTF-8 and try to replace the unknown text below.

" you�"

varlistlabela = spss.GetVariableLabel(var)
varlistlabela=varlistlabela.decode("cp1252").replace(r'[\u0020-\ud7ff]',"").encode("cp1252") 

Upvotes: 0

Views: 397

Answers (1)

Austin
Austin

Reputation: 26039

You need a regex substitution:

re.sub(r'[^\u0020-\ud7ff]', '', s)

where s is the input string.

Code:

import re

s = " you�"
print(re.sub(r'[^\u0020-\ud7ff]', '', s))
#  you

Upvotes: 1

Related Questions