Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแปลง Bytes เป็น Python String

คุณต้อง decode bytes object เพื่อผลิต string สามารถทำได้โดยใช้ฟังก์ชัน decode จาก string class ที่จะรับ encoding ที่คุณต้องการ decode

ตัวอย่าง

my_str = b"Hello"  # b หมายถึง byte string
new_str = my_str.decode('utf-8')  # Decode ด้วย utf-8 encoding
print(new_str)

ผลลัพธ์

Hello