September 5th, 2012, 10:34 AM
String encoding problem
I have a string that is an output from Windows console that looks like this when I print it or write to file:
" C o n f i g M a n a g e r E r r o r C o d e = 0 ".
I guess it's something with encoding, but I don't know how to fix it. I've tried:
mystring = mystring.decode('ascii')
but none of those worked.
mystring = mystring.encode('ascii', 'ignore')
I have also tried simply to replace spaces with nothing:
...but that didn't work too. I guess this string uses two bytes per character (UTF-16?).
mystring = mystring.replace(' ', '')
All I need is to get ASCII string to check if it begins with: "ConfigManagerErrorCode=", but none of the options I've tried converts my string to that format, so my comparison doesn't work.
Am I right about root cause (encoding) and what can I do to fix this?
September 5th, 2012, 11:15 AM
for c in mystring:
and also try the same stuff after encoding and decoding.
That might reveal a clue to this mystery.
[/code] are essential for python code and Makefiles!
September 16th, 2012, 06:49 AM
Thanks for your hint, it helped me solve the problem. It showed me that every second byte is 0, and after I removed them from my string, comparing strings worked as expected again.