0

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") 

1
  • Regular str.replace doesn't understand character ranges like [\u0020-\ud7ff]. Were you thinking of re.sub? Commented Apr 18, 2019 at 2:43

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Just curious, is there a range of code points that represents the "unknown" characters? I see that chr(65533) and chr(65534) are two different things, while chr(65535) seems to be a repeat of the latter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.