📜  Python bytes() 方法

📅  最后修改于: 2022-05-13 01:54:26.399000             🧑  作者: Mango

Python bytes() 方法

Python byte()函数将对象转换为给定大小和数据的不可变字节表示的对象。

Python字节()示例

示例 1:将字符串转换为字节

 在此示例中,我们将使用Python的 bytes()函数将字符串转换为字节,为此,我们将一个带有字符串的变量传递给带有 UTF-8 参数的 bytes()函数。 UTF-8 能够使用一到四个一字节代码单元对 Unicode 中的所有 1,112,064 个有效字符代码点进行编码

Python3
# python code demobstrating
# int to bytes
str = "Welcome to Geeksforgeeks"
 
arr = bytes(str, 'utf-8')
 
print(arr)


Python3
# python code to demonstrate
# int to bytes
 
number = 12
result = bytes(number)
 
print(result)


Python3
print(bytes())


Python3
# Python 3 code to demonstrate the
# working of bytes() on int, iterables, none
 
# initializing integer and iterables
a = 4
lis1 = [1, 2, 3, 4, 5]
 
# No argument case
print ("Byte conversion with no arguments : " + str(bytes()))
 
# conversion to bytes
print ("The integer conversion results in : "  + str(bytes(a)))
print ("The iterable conversion results in : "  + str(bytes(lis1)))


Python3
# Python 3 code to demonstrate the
# working of bytes() on string
 
# initializing string
str1 = 'GeeksfÖrGeeks'
 
# Giving ascii encoding and ignore error
print("Byte conversion with ignore error : " +
      str(bytes(str1, 'ascii', errors='ignore')))
 
# Giving ascii encoding and replace error
print("Byte conversion with replace error : " +
      str(bytes(str1, 'ascii', errors='replace')))
 
# Giving ascii encoding and strict error
# throws exception
print("Byte conversion with strict error : " +
      str(bytes(str1, 'ascii', errors='strict')))


输出:

b'Welcome to Geeksforgeeks'

示例 2:来自整数的字节数组

在这个例子中,我们将看到如何使用Python bytes()函数从整数中获取字节数组,为此我们将整数传递给 bytes()函数。

Python3

# python code to demonstrate
# int to bytes
 
number = 12
result = bytes(number)
 
print(result)

输出:

b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

示例 3:带有 bytes() 的空参数

当我们在 bytes()函数中不传递任何内容时,它会创建一个大小为 0 的数组。

Python3

print(bytes())

输出:

b''

示例 4:在整数、无和可迭代对象上演示 byte()

Python3

# Python 3 code to demonstrate the
# working of bytes() on int, iterables, none
 
# initializing integer and iterables
a = 4
lis1 = [1, 2, 3, 4, 5]
 
# No argument case
print ("Byte conversion with no arguments : " + str(bytes()))
 
# conversion to bytes
print ("The integer conversion results in : "  + str(bytes(a)))
print ("The iterable conversion results in : "  + str(bytes(lis1)))

输出:

Byte conversion with no arguments : b''
The integer conversion results in : b'\x00\x00\x00\x00'
The iterable conversion results in : b'\x01\x02\x03\x04\x05'

带有字符串的字节的行为

字节接受一个字符串作为参数,并需要一个编码方案来执行它。最重要的方面是在编码失败的情况下处理错误,定义的一些错误处理方案是:

示例使用字符串演示 bytes()

Python3

# Python 3 code to demonstrate the
# working of bytes() on string
 
# initializing string
str1 = 'GeeksfÖrGeeks'
 
# Giving ascii encoding and ignore error
print("Byte conversion with ignore error : " +
      str(bytes(str1, 'ascii', errors='ignore')))
 
# Giving ascii encoding and replace error
print("Byte conversion with replace error : " +
      str(bytes(str1, 'ascii', errors='replace')))
 
# Giving ascii encoding and strict error
# throws exception
print("Byte conversion with strict error : " +
      str(bytes(str1, 'ascii', errors='strict')))

输出:

Byte conversion with ignore error : b'GeeksfrGeeks'
Byte conversion with replace error : b'Geeksf?rGeeks'

例外 :