showkey
showkey

Reputation: 348

change between string and bytes in python3

In python 2.7:

>>> x1= '\xba\xba'
>>> x2=b'\xba\xba'
>>> x1==x2
True

In python 3.4:

>>> x1='\xba\xba'
>>> x2=b'\xba\xba'
>>> x1==x2
False
>>> type(x1)
<class 'str'>
>>> type(x2)
<class 'bytes'>
  1. how to change x1 into x2?
  2. how to change x2 into x1?

Upvotes: 0

Views: 784

Answers (2)

falsetru
falsetru

Reputation: 368904

In Python 3.x, use str.encode (str -> bytes) and bytes.decode (bytes -> str) with latin1 encoding (or iso-8859-1):

>>> x1 = '\xba\xba'
>>> x2 = b'\xba\xba'
>>> x1.encode('latin1') == x2
True
>>> x2.decode('latin1') == x1
True

Upvotes: 1

jfs
jfs

Reputation: 414079

Both x1 and x2 are bytestrings in Python 2. If you compare Unicode and bytes on Python 2; you also get False in this case:

>>> u'\xba\xba' == b'\xba\xba'
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False

x1 is a Unicode string in Python 3.

You could add from __future__ import unicode_literals to make the code work the same on both Python 2 and 3:

>>> from __future__ import unicode_literals
>>> x1 = '\xab\xab'
>>> type(x1)
<type 'unicode'>

Don't mix bytestrings and Unicode strings.

To convert Unicode string to bytes:

bytestring = unicode_string.encode(character_encoding)

To convert bytes to Unicode string:

unicode_string = bytestring.decode(character_encoding)

Upvotes: 1

Related Questions