#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    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:
    Code:
    mystring = mystring.decode('ascii')
    and
    Code:
    mystring = mystring.encode('ascii', 'ignore')
    but none of those worked.

    I have also tried simply to replace spaces with nothing:
    Code:
    mystring = mystring.replace(' ', '')
    ...but that didn't work too. I guess this string uses two bytes per character (UTF-16?).

    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?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    try

    print(len(mystring))
    for c in mystring:
    print(ord(c))

    and also try the same stuff after encoding and decoding.
    That might reveal a clue to this mystery.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo