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
bytes
bao gồm các chuỗi các giá trị không dấu 8 bit, trong khi str
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ù bytes
và str
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 bytes
và str
không thể được sử dụng cùng với các nhà khai thác 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
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. Ngoài ra, hãy nhớ rằng so sánh các trường hợp bytes
và str
cho sự bình đẳng, tức là sử dụng >>> # 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
4, sẽ luôn đánh giá thà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
5 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ý bytes
và str
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
>>> # 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
8. 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 ý về 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ố >>> # 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
9 để tránh bất ngờ.Trong Python 2
str
bao gồm các chuỗi của các giá trị 8 bit, trong khi
s = 'Hello world'
# Encoding the string into bytes
bytes_obj = s.encode['ASCII']
print[bytes_obj]
# Output:
b'Hello world'
1 bao gồm các chuỗi các ký tự unicode. Một điều cần lưu ý là str
và s = 'Hello world'
# Encoding the string into bytes
bytes_obj = s.encode['ASCII']
print[bytes_obj]
# Output:
b'Hello world'
1 có thể được sử dụng cùng với các toán tử nếu str
chỉ bao gồm các ký tự ASCI 7 bit.Có thể hữu ích khi sử dụng các hàm trợ giúp để chuyển đổi giữa str
và
s = 'Hello world'
# Encoding the string into bytes
bytes_obj = s.encode['ASCII']
print[bytes_obj]
# Output:
b'Hello world'
1 trong Python 2 và giữa bytes
và str
trong Python 3. Có những lúc bạn bị nhầm lẫn giữa các đối tượng byte và chuỗi. Nhưng có một số khác biệt giữa họ. Hãy thảo luận về sự khác biệt giữa chúng:
Sợi dây
Chuỗi là chuỗi các ký tự. Họ có thể đọc được con người. Chúng không thể được lưu trữ trực tiếp trên đĩa, bạn phải mã hóa chúng thành định dạng có thể đọc được bằng máy là byte.
9s = 'Hello world' # Encoding the string into bytes bytes_obj = s.encode['ASCII'] print[bytes_obj] # Output: b'Hello world'
Mặc định cho các lỗi là 'nghiêm ngặt', có nghĩa là các lỗi mã hóa làm tăng unicodeerror.
Có nhiều hình thức mã hóa khác nhau như PNG, MP3, ASCII, UTF-8, v.v. được sử dụng để thể hiện hình ảnh, âm thanh, văn bản, v.v. bằng byte. Kỹ thuật mặc định là
# Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
0. Hãy lấy một ví dụ để chuyển đổi một chuỗi thành byte:s = 'Hello world'
# Encoding the string into bytes
bytes_obj = s.encode['ASCII']
print[bytes_obj]
# Output:
b'Hello world'
Trong ví dụ trên, chúng tôi đã chuyển đổi chuỗi thành byte bằng phương pháp
# Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
1. Phương thức # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
1 lấy loại mã hóa làm đối số. Ở đây, # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
3 đại diện cho chuỗi trong byte ở dạng ASCII. Phương thức # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
1 trả về đối tượng byte.Đối tượng byte
Các đối tượng byte là các chuỗi bất biến của byte, nghĩa là các số nguyên trong phạm vi 0 đến 255. Byte có thể được lưu trữ trực tiếp trên đĩa. Chúng có thể đọc được bằng máy, bạn phải giải mã chúng thành định dạng có thể đọc được của con người là một chuỗi. Nếu bạn muốn nó trở lại dạng ban đầu thì bạn phải giải mã nó.
5# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode['ASCII'] print[s] # Output: 'Hello world'
Hãy lấy một ví dụ để chuyển đổi byte thành chuỗi:
# Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
Trong ví dụ trên, chúng tôi đã chuyển đổi các byte thành các chuỗi bằng phương pháp
# Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
6. Phương thức # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
6 lấy loại mã hóa làm đối số. Ở đây, # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
8 đại diện cho chuỗi ở dạng ASCII. Phương thức # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
6 trả về một chuỗi.Các đối tượng giống như byte có thể được sử dụng trong các hoạt động khác nhau và phải ở dạng nhị phân như truyền tệp, lập trình ổ cắm, v.v.
Sự kết luận
Trong bài viết này, bạn đã học được sự khác biệt giữa các đối tượng byte và chuỗi trong Python. Chúng tôi cũng đã đề cập đến các phương pháp
# Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
1 và # Byte Object
bytes_obj = b'Hello world'
# Decoding the bytes into string
s = bytes_obj.decode['ASCII']
print[s]
# Output:
'Hello world'
6. Mã hóa và giải mã là hoạt động nghịch đảo. Trước khi lưu trữ dữ liệu trên máy tính, trước tiên bạn phải mã hóa nó. Trước khi đọc dữ liệu từ máy tính, trước tiên bạn phải giải mã nó.