简体   繁体   English

在python3中的字符串和字节之间切换

[英]change between string and bytes in python3

In python 2.7: 在python 2.7中:

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

In python 3.4: 在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 ? 如何将x1更改为x2
  2. how to change x2 into x1 ? 如何将x2更改为x1

In Python 3.x, use str.encode (str -> bytes) and bytes.decode (bytes -> str) with latin1 encoding (or iso-8859-1): 在Python 3.x中,使用str.encode (STR - >字节)和bytes.decode (字节- > STR)与latin1编码(或ISO-8859-1):

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

Both x1 and x2 are bytestrings in Python 2. If you compare Unicode and bytes on Python 2; x1x2都是Python x2中的字节串。 you also get False in this case: 在这种情况下,您也会得到False

>>> 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. x1是Python 3中的Unicode字符串。

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添加from __future__ import unicode_literals以使代码在Python 2和3上均相同:

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

Don't mix bytestrings and Unicode strings. 不要混合字节字符串和Unicode字符串。

To convert Unicode string to bytes: 要将Unicode字符串转换为字节:

bytestring = unicode_string.encode(character_encoding)

To convert bytes to Unicode string: 要将字节转换为Unicode字符串:

unicode_string = bytestring.decode(character_encoding)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM