Issue
What is the best way to compare a string object to a bytes object that works in both Python 2 and Python 3? Assume both are UTF-8. More generally, how does one write a Python 2 and Python 3 compatible comparison of two objects that may each be a string, bytes, or Unicode object?
The problem is that "asdf" == b"asdf"
is True in Python 2 and False in Python 3.
Meanwhile, one cannot blindly encode or decode objects, since strings in Python 2 have both encode
and decode
methods, but strings in Python 3 just have encode methods.
Finally, isinstance(obj, bytes)
returns True for any non-unicode string in Python 2 and returns True for only bytes objects in Python 3.
Solution
In both Python 2 and Python 3, anything that is an instance of bytes
has a decode method. Thus, you can do the following:
def compare(a, b, encoding="utf8"):
if isinstance(a, bytes):
a = a.decode(encoding)
if isinstance(b, bytes):
b = b.decode(encoding)
return a == b
Answered By - Zags
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.