Home Backend Development Python Tutorial A simple way to read and write binary files using Python

A simple way to read and write binary files using Python

Feb 23, 2017 am 11:22 AM

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)
Copy after login

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.


##FormatC TypePythonNumber of bytesxpad byteno value1ccharstring of length 11##bB?hHiIl##Lunsigned longlong4long longunsigned long longfloatdoublechar[]char[]void *In order to exchange data with structures in c, it is also necessary to consider that some c or c++ compilers use byte alignment, usually 4 A 32-bit system in which bytes are used, so
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
##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


##Character

Byte orderSize and alignment##@nativenative Make up enough 4 bytes=nativestandard    According to the original number of bytes<little-endianstandard                                                                                     can be based on the original number of bytes >big-endian##!network (= big-endian)standard Based on the original number of bytes

The method of use is to put it in the first position of fmt, just like '@5s6sif'

-----Problems encountered when processing binary files--- --

When we process binary files, we need to use the following method

binfile=open(filepath,'rb') to read binary files

or

binfile=open(filepath,'wb') Write binary file

So what is the difference between the results of binfile=open(filepath,'r') and binfile=open(filepath,'r')?

There are two differences:

First, if you encounter '0x1A' when using 'r', it will be regarded as the end of the file, which is EOF. Using 'rb' does not have this problem. That is, if you write in binary and read out in text, only part of the file will be read out if '0X1A' is present. When using 'rb', it will read to the end of the file.

Second, for the string x='abc/ndef', we can use len(x) to get its length to be 7. /n is called a newline character, which is actually '0X0A'. When we use 'w' to write in text mode, '0X0A' will be automatically changed into two characters '0X0D' and '0X0A' on the Windows platform, that is, the file length actually becomes 8. When reading in 'r' text mode, it is automatically converted to the original newline character. If you change to 'wb' binary mode to write, one character will remain unchanged, and it will be read as it is when reading. So if you write in text mode and read in binary mode, you have to consider this extra byte. '0X0D' is also called the carriage return character.
It will not change under Linux. Because linux only uses '0X0A' to represent line breaks.

The above simple method (recommended) for reading and writing binary files using Python is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more related articles on simple methods of reading and writing binary files using Python, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

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 by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

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 in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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 without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

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...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

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)...

See all articles