A simple way to read and write binary files using Python
The general feeling is that python itself does not support binary, but it provides a module to make up for it, which is the struct module.
Python does not have a binary type, but it can store binary type data, that is, use the string string type to store binary data. This does not matter, because string is based on 1 byte.
import struct
a=12.34
#Convert a into binary
bytes=struct.pack('i',a)
At this time, bytes is a string string. The string is the same as the binary storage content of a in bytes.
Reverse operation
Existing binary data bytes, (actually a string), convert it back into python data type:
a,=struct .unpack('i',bytes)
Note that unpack returns a tuple
So if there is only one variable:
bytes=struct.pack('i' ,a)
Then, you need to do this when decoding
a,=struct.unpack('i',bytes) or (a,)=struct.unpack('i',bytes )
If you use a=struct.unpack('i',bytes) directly, then a=(12.34,) is a tuple instead of the original floating point number.
If it is composed of multiple data, it can be like this:
a='hello' b='world!' c=2 d=45.123 bytes=struct.pack('5s6sif',a,b,c,d)
The bytes at this time are data in binary form, and you can directly Write a file such as binfile.write(bytes)
Then, when we need it, we can read it out, bytes=binfile.read()
and then decode it into python through struct.unpack() Variable
a,b,c,d=struct.unpack('5s6sif',bytes)
'5s6sif' is called fmt, which is a format string, consisting of numbers and characters. 5s represents a string of 5 characters, 2i represents 2 integers, etc. The following are the available characters and types. ctype represents a one-to-one correspondence with the types in python.
C Type | Python | Number of bytes | |
---|---|---|---|
pad byte | no value | 1 | |
char | string of length 1 | 1 | |
signed char | integer | 1 | |
unsigned char | integer | 1 | |
_Bool | bool | 1 | |
short | integer | 2 | |
unsigned short | integer | 2 | |
int | integer | 4 | |
unsigned int | integer or long | 4 | |
long | integer | 4 | ##L |
long | 4 | ##q | |
long | 8 | Q | |
long | 8 | f | |
float | 4 | d | |
float | 8 | s | |
string | 1 | p | |
string | 1 | P | |
long |
##Last one Can be used to represent pointer types, occupying 4 bytes |
##@ | native | |
---|---|---|
= | native | standard According to the original number of bytes |
< | little-endian | standard can be based on the original number of bytes |
> | big-endian | |
network (= big-endian) | standard Based on the original number of bytes | |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
