Python3 Medium encode and decode
Python3 Character encoding is often used in decode and encode function . Especially in crawling web pages , It's good to be proficient with these two functions .encode The role of , So that we can see the intuitive character into the computer byte form .decode Just the opposite , Convert the characters in byte form into what we can understand 、 Intuitive 、“ Human model ” In the form of .
str convert to bytes:
The first way :charArray = bytes(data.encode('utf-8')) The second way :charArray = b"test" # b" " A prefix : The following string is bytes type . The third way :array.array('u', data)
bytes convert to str:
bytes_var.decode("utf-8").split(",")