Python - string encoding
I have a simple python script that takes the selected text, reverses it, and replaces the selection with the reversed string
With standard English letters, it works perfectly, but when I have Hebrew letters selected, some characters don’t get entered correctly (I get some binary hex characters e.g. xA3 in a blue box)
The document is encoded with OEM 862 encoding
Here is the script that I use
text = editor.getSelText() editor.replaceSel(text[::-1])
If anyone can help with this I would appreciate it
did you read this?
Hi Claudia - Thanks for the link!
I did read about that (but not there, rather on stackoverflow.com), and I’ve tried and tried but haven’t been able to figure out what to do!
Now that you pointed to it again, I put more time into it… and finally I figured out a method to get it to work!
However, I have no clue how it works!
This is what I have:
- A text file encoded in OEM 862 (or in python ‘cp862’)
- A script with default encoding (ANSI)
So, I would think that I have to do some type of encoding with cp862 and utf-8 (since python works in utf-8).
text.decode('cp862') text.encode('cp862') text.decode('cp862') text.encode('utf-8') text.decode('utf-8') text.encode('cp862')
None of the above worked!
Now, I tried the exact example given in the help file you pointed to above:
And this worked perfectly!
This is great! My script is now working!
But… Why?! why does this make sense??
I am so confused with this encoding business…! :)
what should I say, it’s like the regexes.
Whenever you think you finally understand its behaviour
something happens which you didn’t expect.
Tbh, I can’t give you any advice on this as I’m still in the same situation as you - why does it what it does. ;-)
But, c’mon - be happy it works and there will be a tomorrow when it fails again,
so take the chance to celebrate - hip hip horroray ;-)
:) :) :)
Thanks Claudia - you made me laugh!
I just hope it doesnt happen any time soon…! :)
you made me laugh!
Well, then my work is done - time to party