Search code examples
pythonstringencodingctypes

How to compare two strings of different encoding in python?


Context:

I'm extracting a string from a txt file encoded in 'utf-8', application_name = 'MicrosoftEdge'

Then I'm using python ctypes module to determine the current active application window = curr_application

user32 = ctypes.WinDLL('user32', use_last_error=True)
curr_window = user32.GetForegroundWindow()
window_name = str(win32gui.GetWindowText(curr_window))
rev = window_name[::-1]
pos = rev.find("-")
curr_application = rev[0:pos][::-1].replace(" ","")

which also returns: 'MicrosoftEdge'

but when I do:

print(curr_application == application_name)

it always returns False

Here's the output I got from:

>>> print(application_name.encode())
b'MicrosoftEdge\n'
>>> print(curr_application.encode())
b'Microsoft\xe2\x80\x8bEdge'

My question is, what should I do so that I get true when I compare both strings?


Solution

  • UPDATE:

    Here's what worked for me:

    import string
    allowed_chars = string.ascii_letters
    application_name = 'MicrosoftEdge'
    curr_application = 'Microsoft\xe2\x80\x8bEdge'
    application = ""
    for letter in curr_application:
        if letter in allowed_chars:
            application = application + letter
    print(application==application_name)
    

    which then returns True