预计阅读本页时间:-
Conversions
Although Python 2.X allowed str and unicode type objects to be mixed freely (if the strings contained only 7-bit ASCII text), 3.0 draws a much sharper distinction—str and bytes type objects never mix automatically in expressions and never are converted to one another automatically when passed to functions. A function that expects an argument to be a str object won’t generally accept a bytes, and vice versa.
Because of this, Python 3.0 basically requires that you commit to one type or the other, or perform manual, explicit conversions:
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
- str.encode() and bytes(S, encoding) translate a string to its raw bytes form and create a bytes from a str in the process.
- bytes.decode() and str(B, encoding) translate raw bytes into its string form and create a str from a bytes in the process.
These encode and decode methods (as well as file objects, described in the next section) use either a default encoding for your platform or an explicitly passed-in encoding name. For example, in 3.0:
>>> S = 'eggs'
>>> S.encode() # str to bytes: encode text into raw bytes
b'eggs'
>>> bytes(S, encoding='ascii') # str to bytes, alternative
b'eggs'
>>> B = b'spam'
>>> B.decode() # bytes to str: decode raw bytes into text
'spam'
>>> str(B, encoding='ascii') # bytes to str, alternative
'spam'
Two cautions here. First of all, your platform’s default encoding is available in the sys module, but the encoding argument to bytes is not optional, even though it is in str.encode (and bytes.decode).
Second, although calls to str do not require the encoding argument like bytes does, leaving it off in str calls does not mean it defaults—instead, a str call without an encoding returns the bytes object’s print string, not its str converted form (this is usually not what you’ll want!). Assuming B and S are still as in the prior listing:
>>> import sys
>>> sys.platform # Underlying platform
'win32'
>>> sys.getdefaultencoding() # Default encoding for str here
'utf-8'
>>> bytes(S)
TypeError: string argument without an encoding
>>> str(B) # str without encoding
"b'spam'" # A print string, not conversion!
>>> len(str(B))
7
>>> len(str(B, encoding='ascii')) # Use encoding to convert to str
4