在这个比较函数中,我遇到了一些笨拙的代码:
def compare(this, that, encoding="utf-8"):
if type(this) == type(str()):
this = this.encode(encoding)
if type(this) == type(bytes()):
this = this.decode().encode(encoding)
if type(that) == type(str()):
that = that.encode(encoding)
if type(that) == type(bytes()):
that = that.decode().encode(encoding)
# there has to be a faster way...
return this==that目的是在进行比较之前确保this和that在相同的编码中。但这似乎是一种尴尬的方式。有没有更简洁的方法?
发布于 2014-01-12 11:15:46
我认为最简单的方法是将这两个输入转换为str并进行比较。
def compare(this, that, encoding="utf-8"):
// convert this to str
if isinstance(this, bytes):
this = str(this, encoding)
// convert that to str
if isinstance(that, bytes):
that = str(that, encoding)
return this == thathttps://stackoverflow.com/questions/21070922
复制相似问题