Hướng dẫn python 3 bytes to string - python 3 byte thành chuỗi

Nếu bạn không biết mã hóa, thì hãy đọc đầu vào nhị phân vào chuỗi trong Python 3 và Python 2 cách tương thích, hãy sử dụng mã hóa MS-DOS CP437 cổ đại:

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('cp437'))

Bởi vì mã hóa chưa được biết, mong đợi các ký hiệu không phải tiếng Anh dịch sang các ký tự của

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
6 (ký tự tiếng Anh không được dịch, bởi vì chúng khớp với hầu hết các mã hóa byte đơn và UTF-8).

Giải mã đầu vào nhị phân tùy ý cho UTF-8 không an toàn, bởi vì bạn có thể nhận được điều này:

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte

Điều tương tự cũng áp dụng cho

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
7, rất phổ biến (mặc định?) Đối với Python 2. Xem các điểm còn thiếu trong bố cục codePage - đó là nơi Python bị nghẹn với
>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
8 khét tiếng.

Cập nhật 20150604: Có tin đồn rằng Python 3 có chiến lược lỗi

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
9 để mã hóa các công cụ thành dữ liệu nhị phân mà không bị mất dữ liệu và sự cố, nhưng nó cần kiểm tra chuyển đổi,
PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
0, để xác thực cả hiệu suất và độ tin cậy.
: There are rumors that Python 3 has the
>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
9 error strategy for encoding stuff into binary data without data loss and crashes, but it needs conversion tests,
PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
0, to validate both performance and reliability.

CẬP NHẬT 20170116: Nhờ bình luận của Nearoo - cũng có khả năng cắt giảm Escape tất cả các byte chưa biết với trình xử lý lỗi

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
1. Điều đó chỉ hoạt động cho Python 3, vì vậy ngay cả với cách giải quyết này, bạn vẫn sẽ nhận được đầu ra không nhất quán từ các phiên bản Python khác nhau:: Thanks to comment by Nearoo - there is also a possibility to slash escape all unknown bytes with
PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
1 error handler. That works only for Python 3, so even with this workaround you will still get inconsistent output from different Python versions:

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))

Xem Hỗ trợ Unicode của Python để biết chi tiết.

CẬP NHẬT 20170119: Tôi quyết định thực hiện giải mã Escaping Slash hoạt động cho cả Python & NBSP; 2 và Python & NBSP; 3. Nó nên chậm hơn giải pháp

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
6, nhưng nó sẽ tạo ra kết quả giống hệt nhau trên mỗi phiên bản Python.: I decided to implement slash escaping decode that works for both Python 2 and Python 3. It should be slower than the
>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
6 solution, but it should produce identical results on every Python version.

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))

Giới thiệu

Trong bài viết này, chúng ta sẽ xem xét cách chuyển đổi byte thành một chuỗi trong Python. Đến cuối bài viết này, bạn sẽ có một ý tưởng rõ ràng về những loại này là gì và làm thế nào để xử lý dữ liệu một cách hiệu quả bằng cách sử dụng chúng.

Tùy thuộc vào phiên bản Python bạn đang sử dụng, nhiệm vụ này sẽ khác nhau. Mặc dù Python 2 đã kết thúc cuộc đời, nhiều dự án vẫn sử dụng nó, vì vậy chúng tôi sẽ bao gồm cả hai phương pháp Python 2 và Python 3.

Chuyển đổi byte thành chuỗi trong Python 3

Kể từ Python 3, cách làm ascii cũ phải đi, và Python trở nên hoàn toàn đơn giản.

Điều này có nghĩa là chúng tôi đã mất loại unicode rõ ràng:

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
3 - Mỗi chuỗi là
PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
3!

Để phân biệt các chuỗi này với các bytests cũ tốt, chúng tôi được giới thiệu với một nhà xác định mới cho chúng -

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
5.

Điều này đã được thêm vào Python 2.6, nhưng nó không phục vụ mục đích thực sự nào ngoài việc chuẩn bị cho Python 3 vì tất cả các chuỗi đều là bytestrings trong 2.6.

Bytestrings trong Python 3 chính thức được gọi là

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
6, một chuỗi số nguyên bất biến trong phạm vi 0

Chuyển đổi byte thành chuỗi bằng giải mã ()

Chúng ta hãy xem cách chúng ta có thể chuyển đổi byte thành một chuỗi, sử dụng phương thức

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
0 tích hợp cho lớp
PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
6:

>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"

Chuyển định dạng mã hóa, chúng tôi đã giải mã đối tượng

PY3K = sys.version_info >= (3, 0)

lines = []
for line in stream:
    if not PY3K:
        lines.append(line)
    else:
        lines.append(line.decode('utf-8', 'backslashreplace'))
6 thành một chuỗi và in nó.

Chuyển đổi byte thành chuỗi với codecs

Ngoài ra, chúng tôi cũng có thể sử dụng mô-đun

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
3 tích hợp cho mục đích này:

>>> import codecs
>>> b = b'Lets grab a \xf0\x9f\x8d\x95!'

>>> codecs.decode(b, 'UTF-8')
"Let's grab a 🍕!"

Tuy nhiên, bạn không thực sự cần phải vượt qua tham số mã hóa

>>> codecs.decode(b)
"Let's grab a 🍕!"

Chuyển đổi byte thành chuỗi bằng str ()

Cuối cùng, bạn có thể sử dụng hàm

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
4, chấp nhận các giá trị khác nhau và chuyển đổi chúng thành các chuỗi:

>>> b = b'Lets grab a \xf0\x9f\x8d\x95!'
>>> str(b, 'UTF-8')
"Let's grab a 🍕!"

Đảm bảo cung cấp đối số mã hóa cho

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
4, nếu không, bạn có thể nhận được một số kết quả bất ngờ:

>>> str(b)
b'Lets grab a \xf0\x9f\x8d\x95!'

Điều này đưa chúng ta đến mã hóa một lần nữa. Nếu bạn chỉ định mã hóa sai, trường hợp tốt nhất là chương trình của bạn bị sập vì nó không thể giải mã dữ liệu. Ví dụ: nếu chúng tôi đã thử sử dụng chức năng

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
4 với
# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
7, chúng tôi sẽ được chào đón:

>>> str(b, 'UTF-16')
'敌❴\u2073牧扡愠\uf020趟↕'

Điều này thậm chí còn quan trọng hơn khi Python 3 thích giả định Unicode - vì vậy nếu bạn đang làm việc với các tệp hoặc nguồn dữ liệu sử dụng mã hóa tối nghĩa, hãy đảm bảo chú ý thêm.

Chuyển đổi byte thành chuỗi trong Python 2

Trong Python 2, một bó byte và một chuỗi thực tế là cùng một thứ - các chuỗi là các đối tượng bao gồm các ký tự dài 1 byte, có nghĩa là mỗi ký tự có thể lưu trữ 256 giá trị. Đó là lý do tại sao chúng đôi khi được gọi là bytestrings.bytestrings.

Điều này thật tuyệt khi làm việc với dữ liệu byte - chúng tôi chỉ tải nó vào một biến và chúng tôi đã sẵn sàng để in:

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
0

Sử dụng các ký tự unicode trong bytestrings không thay đổi hành vi này một chút: mặc dù:

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
1

Chuyển đổi byte thành unicode (Python 2)

Ở đây, chúng ta sẽ phải sử dụng loại ____38 của Python 2, được giả định và tự động sử dụng trong Python 3. Điều này lưu trữ các chuỗi dưới dạng một loạt các điểm mã, thay vì byte.

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
9 đại diện cho các byte là số HEX hai chữ số như Python không biết cách đại diện cho chúng dưới dạng các ký tự ASCII:

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
2

Như bạn có thể thấy ở trên, chuỗi Unicode chứa

>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"
0 - một ký tự thoát ra Unicode mà thiết bị đầu cuối của chúng tôi bây giờ biết cách in ra như một lát bánh pizza! Cài đặt điều này dễ dàng như sử dụng bộ xác định
>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"
1 trước giá trị của byteString.

Vì vậy, làm thế nào để tôi chuyển đổi giữa hai?

Kiểm tra hướng dẫn thực hành của chúng tôi, thực tế để học Git, với các thực hành tốt nhất, các tiêu chuẩn được công nghiệp chấp nhận và bao gồm bảng gian lận. Ngừng các lệnh git googling và thực sự tìm hiểu nó!

Bạn có thể nhận được chuỗi unicode bằng cách giải mã bytestring của bạn. Điều này có thể được thực hiện bằng cách xây dựng một đối tượng Unicode, cung cấp bytestring và một chuỗi chứa tên mã hóa dưới dạng đối số hoặc bằng cách gọi

>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"
2 trên một bytestring.

Chuyển đổi byte thành chuỗi bằng Decode () (Python 2)

Bạn cũng có thể sử dụng

>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"
3 từ mô -đun
# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
3.

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
3

Chuyển đổi byte thành chuỗi bằng cách sử dụng codecs (Python 2)

Hoặc, sử dụng mô -đun

# --- preparation

import codecs

def slashescape(err):
    """ codecs error handler. err is UnicodeDecode instance. return
    a tuple with a replacement for the unencodable part of the input
    and a position where encoding should continue"""
    #print err, dir(err), err.start, err.end, err.object[:err.start]
    thebyte = err.object[err.start:err.end]
    repl = u'\\x'+hex(ord(thebyte))[2:]
    return (repl, err.end)

codecs.register_error('slashescape', slashescape)

# --- processing

stream = [b'\x80abc']

lines = []
for line in stream:
    lines.append(line.decode('utf-8', 'slashescape'))
3:

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
4

Hãy chú ý đến mã hóa của bạn

Một lời cảnh báo ở đây - byte có thể được giải thích khác nhau trong các mã hóa khác nhau. Với khoảng 80 mã hóa khác nhau có sẵn ngoài hộp, có thể không dễ để biết nếu bạn có đúng!80 different encodings available out of the box, it might not be easy to know if you've got the right one!

>>> b'\x00\x01\xffsd'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 2: invalid
start byte
5

Thông báo ban đầu là

>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"
6 hoặc
>>> b = b"Lets grab a \xf0\x9f\x8d\x95!"
# Let's check the type
>>> type(b)
<class 'bytes'>

# Now, let's decode/convert them into a string
>>> s = b.decode('UTF-8')
>>> s
"Let's grab a 🍕!"
7 và cả hai dường như là chuyển đổi hợp lệ.

Sự kết luận

Là lập trình viên, có một số điều chúng ta phải liên tục nghĩ đến và tích cực chuẩn bị để tránh những cạm bẫy. Điều này đặc biệt đúng ở cấp độ thấp hơn, nơi chúng ta hiếm khi đi khi chúng ta sử dụng một ngôn ngữ cấp cao như Python làm trình điều khiển hàng ngày của chúng ta.

Những thứ như ký tự, mã hóa và nhị phân ở đó để nhắc nhở chúng ta rằng công việc của chúng ta là mã hóa - mã hóa suy nghĩ của chúng ta thành các giải pháp làm việc. Rất may, rất nhiều suy nghĩ này trở thành một phần của thói quen của chúng tôi sau một vài vòng tại bàn phím.code - to encode our thoughts into working solutions. Thankfully, a lot of this thinking becomes part of our routine after a few rounds at the keyboard.

Trong bài viết này, chúng tôi đã đi qua cách chuyển đổi byte thành chuỗi trong Python.

Có bao nhiêu byte là một chuỗi trong Python?

Lưu ý rằng mỗi chuỗi trong Python có thêm 49-80 byte bộ nhớ, trong đó nó lưu trữ thông tin bổ sung, chẳng hạn như băm, độ dài, độ dài trong byte, loại mã hóa và cờ chuỗi.Đó là lý do tại sao một chuỗi trống mất 49 byte bộ nhớ.49-80 bytes of memory, where it stores supplementary information, such as hash, length, length in bytes, encoding type and string flags. That's why an empty string takes 49 bytes of memory.

Một chuỗi là bao nhiêu byte?

Nhưng những gì về một chuỗi?Một chuỗi bao gồm: tiêu đề đối tượng 8 byte (đồng bộ hóa 4 byte và bộ mô tả loại 4 byte)8-byte object header (4-byte SyncBlock and a 4-byte type descriptor)

Byte là gì trong Python3?

Hàm python byte () Nó có thể chuyển đổi các đối tượng thành các đối tượng byte hoặc tạo đối tượng byte trống của kích thước được chỉ định.Sự khác biệt giữa byte () và bytearray () là byte () trả về một đối tượng không thể sửa đổi và bytearray () trả về một đối tượng có thể được sửa đổi.It can convert objects into bytes objects, or create empty bytes object of the specified size. The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.

Phương thức nào được sử dụng để chuyển đổi dữ liệu byte thô thành chuỗi a toString () b chuyển đổi () c encode () d decode ()?

Sử dụng phương thức decode () python cung cấp phương thức decode () tích hợp, được sử dụng để chuyển đổi byte thành một chuỗi.decode() method Python provides the built-in decode() method, which is used to convert bytes to a string.