• notice
  • Congratulations on the launch of the Sought Tech site

How Python stores and reads byte data in the form of ASCII code

content
  • Store and read byte data in ASCII code

  • Obtaining Python ASCII code


Store and read byte data in ASCII code

Python can store byte data to txt, but don't store it directly in str, convert it into a list of numbers to store it, so it's easy to read

L = []a = b"x00xefxa2xa0xb3x8bx9dx1exf8x98x19x39xd9x9dxfdthe first linea f""v00"print(a)for each in a:
   L.append(int(each))with open("data.txt","w") as p:
   p.write(str(L))print(L)>>> [0, 239, 162, 160, 179, 139, 157, 30, 248, 152, 25, 57, 217, 157, 253, 116, 104, 101, 32, 102, 105, 114, 115, 116, 32, 108, 105, 110, 101, 10, 13, 7, 8, 9, 92, 12, 39, 34, 11, 8, 10, 0]

with open("data.txt","r") as p:
   line = p.readline()print(b"".join([bytes([int(i)]) for i in line[1:-1].split(",")]))>>> b"x00xefxa2xa0xb3x8bx9dx1exf8x98x199xd9x9dxfdthe first linex07x08 x0c""x0bx08x00"


Obtaining Python ASCII code

The ord function can get the ASCII code of the character, the usage is as follows:

Code:

#ord('character") can return the ASCII code of the characterprint(ord("a"))

operation result:

insert image description here

The above is my personal experience, I hope it can give you a reference, and I hope you will support the Yunhaitian tutorial a lot.

Original address: https://blog.csdn.net/weixin_40222586/article/details/102948919


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+