Hướng dẫn how does bytes work in python? - byte hoạt động như thế nào trong python?

Lưu ý: Tôi sẽ giải thích nhiều hơn câu trả lời của mình cho Python 3 vì kết thúc cuộc đời của Python 2 rất gần. I will elaborate more my answer for Python 3 since the end of life of Python 2 is very close.

Trong Python 3

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
0 bao gồm các chuỗi các giá trị không dấu 8 bit, trong khi
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 bao gồm các chuỗi các điểm mã Unicode đại diện cho các ký tự văn bản từ ngôn ngữ con người.

>>> # bytes
>>> b = b'h\x65llo'
>>> type(b)

>>> list(b)
[104, 101, 108, 108, 111]
>>> print(b)
b'hello'
>>>
>>> # str
>>> s = 'nai\u0308ve'
>>> type(s)

>>> list(s)
['n', 'a', 'i', '̈', 'v', 'e']
>>> print(s)
naïve

Mặc dù

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
0 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 dường như hoạt động theo cùng một cách, các trường hợp của chúng không tương thích với nhau, tức là, các trường hợp
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
0 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 không thể được sử dụng cùng với các toán tử như
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
6 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
7. Ngoài ra, hãy nhớ rằng so sánh các trường hợp ____10 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 cho sự bình đẳng, tức là sử dụng
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
0, sẽ luôn đánh giá thành
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
1 ngay cả khi chúng chứa chính xác cùng một ký tự.

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False

Một vấn đề khác khi xử lý

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
0 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 có mặt khi làm việc với các tệp được trả về bằng hàm tích hợp
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
4. Một mặt, nếu bạn muốn OT đọc hoặc ghi dữ liệu nhị phân vào/từ một tệp, hãy luôn mở tệp bằng chế độ nhị phân như 'RB' hoặc 'WB'. Mặt khác, nếu bạn muốn đọc hoặc ghi dữ liệu Unicode sang/từ một tệp, hãy lưu ý rằng mã hóa mặc định của máy tính của bạn, vì vậy nếu cần phải vượt qua tham số
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
5 để tránh bất ngờ.

Trong Python 2

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 bao gồm các chuỗi của các giá trị 8 bit, trong khi
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
7 bao gồm các chuỗi các ký tự unicode. Một điều cần lưu ý là
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 và
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
7 có thể được sử dụng cùng với các toán tử nếu
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 chỉ bao gồm các ký tự ASCI 7 bit.

Có thể hữu ích khi sử dụng các chức năng trợ giúp để chuyển đổi giữa

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 và
message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'
7 trong Python 2 và giữa
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
0 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1 trong Python 3.

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức python byte () với sự trợ giúp của các ví dụ.

Phương pháp

bytes([source[, encoding[, errors]]])
5 trả về một đối tượng byte bất biến được khởi tạo với kích thước và dữ liệu đã cho.

Thí dụ

message = 'Python is fun'

# convert string to bytes byte_message = bytes(message, 'utf-8')

print(byte_message) # Output: b'Python is fun'


Byte () cú pháp

Phương pháp cú pháp của phương pháp

bytes([source[, encoding[, errors]]])
5 là:

bytes([source[, encoding[, errors]]])

Phương thức

bytes([source[, encoding[, errors]]])
5 Trả về một đối tượng byte là chuỗi số nguyên bất biến (không thể sửa đổi) trong phạm vi
bytes([source[, encoding[, errors]]])
8.

Nếu bạn muốn sử dụng phiên bản có thể thay đổi, hãy sử dụng phương thức bytearray ().


tham số byte ()

bytes([source[, encoding[, errors]]])
5 lấy ba tham số tùy chọn:

  • Nguồn (Tùy chọn) - Nguồn để khởi tạo mảng byte. - source to initialize the array of bytes.
  • Mã hóa (tùy chọn) - Nếu nguồn là một chuỗi, mã hóa chuỗi. - if the source is a string, the encoding of the string.
  • Lỗi (tùy chọn) - Nếu nguồn là một chuỗi, hành động cần thực hiện khi chuyển đổi mã hóa không thành công (đọc thêm: mã hóa chuỗi) - if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)

Tham số nguồn có thể được sử dụng để khởi tạo mảng byte theo các cách sau:source parameter can be used to initialize the byte array in the following ways:

Loại hìnhSự mô tả
Sợi dâyChuyển đổi chuỗi thành byte bằng str.encode () cũng phải cung cấp mã hóa và lỗi tùy chọnencoding and optionally errors
Số nguyênTạo một mảng có kích thước được cung cấp, tất cả được khởi tạo thành null
Sự vậtBộ đệm chỉ đọc của đối tượng sẽ được sử dụng để khởi tạo mảng byte
Có thể lặp lạiTạo một mảng có kích thước bằng với số lượng ITBER và được khởi tạo cho các phần tử có thể lặp lại phải có thể lặp lại của các số nguyên giữa
string = "Python is interesting."

# string with encoding 'utf-8'

arr = bytes(string, 'utf-8')

print(arr)
0
Không có nguồn (đối số)Tạo một mảng có kích thước 0


byte () giá trị trả về

Phương thức

bytes([source[, encoding[, errors]]])
5 trả về đối tượng byte của kích thước và giá trị khởi tạo đã cho.


Ví dụ 1: Chuyển đổi chuỗi thành byte

string = "Python is interesting."

# string with encoding 'utf-8'

arr = bytes(string, 'utf-8')

print(arr)

Đầu ra

b'Python is interesting.'

Ví dụ 2: Tạo một byte có kích thước số nguyên đã cho

size = 5

arr = bytes(size)

print(arr)

Đầu ra

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

Ví dụ 2: Tạo một byte có kích thước số nguyên đã cho

rList = [1, 2, 3, 4, 5]

arr = bytes(rList)

print(arr)

Đầu ra

b'\x01\x02\x03\x04\x05'

Làm thế nào để Python lưu trữ dữ liệu trong byte?

Các đối tượng byte có thể được xây dựng hàm tạo, byte () và từ các chữ;Sử dụng tiền tố B với cú pháp chuỗi bình thường: b'python '.Để xây dựng các mảng byte, hãy sử dụng hàm bytearray ().. To construct byte arrays, use the bytearray() function.

Làm thế nào để bạn viết byte trong Python?

Viết byte vào tệp trong Python Ví dụ 1: Mở tệp ở chế độ ghi nhị phân và sau đó chỉ định nội dung để ghi dưới dạng byte.Tiếp theo, sử dụng chức năng ghi để viết nội dung byte vào tệp nhị phân.pen a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.

Kích thước byte trong Python là gì?

Hàm byte () chuyển đổi một đối tượng (như danh sách, chuỗi, số nguyên, v.v.) thành một đối tượng byte bất biến trong phạm vi từ 0 đến 256. Nếu không có đối tượng nào được cung cấp cho phương thức byte (), nó có thể tạo ra mộtCác byte trống đối tượng của một kích thước cụ thể.0 to 256. If no object is provided to the bytes() method, it can generate an empty bytes object of a specified size.

Kiểu dữ liệu byte trong Python là gì?

Hàm python byte () chuyển đổi một đối tượng thành đối tượng đại diện byte bất biến của kích thước và dữ liệu đã cho.Cú pháp: BYTE (SRC, ENC, ERR) Tham số: SRC: Đối tượng nguồn phải được chuyển đổi.ENC: Mã hóa cần thiết trong trường hợp đối tượng là một chuỗi.immutable byte-represented object of given size and data. Syntax : bytes(src, enc, err) Parameters : src : The source object which has to be converted. enc : The encoding required in case object is a string.