Is there any simple way to get consistent results in both Python 2 and Python 3 for operatioIn like "give me N-th byte in byte string"? Getting either byte-as-integer or byte-as-character will do for me, as long as that will be consistent.
Ie given
s = b"123"
Naïve approach yields:
s[1] # => Python 2: '2', <type 'str'>
s[1] # => Python 3: 50, <class 'int'>
Wrapping that in ord(...)
yields an error in Python 3:
ord(s[1]) # => Python 2: 50, <type 'int'>
ord(s[1]) # => Python 3: TypeError: ord() expected string of length 1, but int found
I can think of a fairly complicated compat solution:
ord(s[1]) if (type(s[1]) == type("str")) else s[1] # 50 in both Python 2 and 3
... but may be there's an easier way which I just don't notice?
A length-1 slice will be also be a byte-sequence in either 2.x or 3.x:
s = b'123'
s[1:2] # 3.x: b'2'; 2.x: '2', which is the same thing but the repr() rules are different.
What about something like this?
import sys
if sys.version_info.major == 3:
def index(s, n):
return s[n]
elif sys.version_info.major == 2:
def index(s, n):
return ord(s[n])
else:
raise NotImplementedError
If you use (converting if needed) the bytearray
type, behavior will be indentical on both version, always matching Python 3 behavior. That's because bytearray
is actually a distinct type on Python 2 (with Python 3 behavior), where bytes
is just an alias for str
there.
The more typical solution would be to use the six
compatibility library, which provides six.indexbytes
, so on either version of Python, you could do:
>>> six.indexbytes(s, 1)
50
Prefix your string with u
and you'll get consistency across Python versions.
# Python 2
>>> ord(u"123"[0])
49
# Python 3
>>> ord(u"123"[0])
49
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.