如何比较python中两个不同编码的字符串?
How to compare two strings of different encoding in python?
上下文:
我正在从 'utf-8', application_name = 'MicrosoftEdge'
编码的 txt 文件中提取字符串
然后我使用 python ctypes 模块来确定当前活动的应用程序 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(" ","")
这也是 returns:'MicrosoftEdge'
但当我这样做时:
print(curr_application == application_name)
总是returnsFalse
这是我从中得到的输出:
>>> print(application_name.encode())
b'MicrosoftEdge\n'
>>> print(curr_application.encode())
b'Microsoft\xe2\x80\x8bEdge'
我的问题是,我应该怎么做才能在比较两个字符串时得到 true?
更新:
以下是对我有用的方法:
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)
然后 returns True
您可以通过将其替换为空字符串来删除该字符:
curr_application.replace('\N{ZERO WIDTH SPACE}', '')
上下文:
我正在从 'utf-8', application_name = 'MicrosoftEdge'
然后我使用 python ctypes 模块来确定当前活动的应用程序 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(" ","")
这也是 returns:'MicrosoftEdge'
但当我这样做时:
print(curr_application == application_name)
总是returnsFalse
这是我从中得到的输出:
>>> print(application_name.encode())
b'MicrosoftEdge\n'
>>> print(curr_application.encode())
b'Microsoft\xe2\x80\x8bEdge'
我的问题是,我应该怎么做才能在比较两个字符串时得到 true?
更新:
以下是对我有用的方法:
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)
然后 returns True
您可以通过将其替换为空字符串来删除该字符:
curr_application.replace('\N{ZERO WIDTH SPACE}', '')