A struct is a built-in module in Python used to convert between a struct in C and a string in Python, typically from a file or network
1. function
(1) Convert Python data to a string in the specified format (the string is a byte stream)
(2) Convert a byte stream to a python-specified data type in the specified format
(3) Processing binary data, if using a struct to process the file, you need to use ' WB '/' RB ' in binary write, read the way to process the file
(4) To deal with the structure in C language
2. Common methods of struct
(1) pack (FMT, V1, v2, ...), bytes
Converts data to a string (byte stream) in the given format
(2) Pack_into (FMT, buffer, offset, v1, v2, ...)
Converts the data to a string (byte stream) in the given format and writes the byte stream to buffer starting at offset
(3) unpack (FMT, buffer) (v1, v2, ...)
Parses a byte stream and returns the result in the given format
(4) unpack_from (fmt, buffer, offset=0) (v1, v2, ...)
Resolves the buffer starting with offset in the specified format and returns the parse result
(5) calcsize (FMT), Integer
Calculates how many bytes of memory a given format takes, and note the alignment
3. Format characters
Formatting strings: Specifying data types, controlling byte order, size, and alignment
The following 2 sheets are from the official website
Character |
Byte Order |
Size |
Alignment |
@ |
Native |
Native |
Enough for 4 bytes. |
= |
Native |
Standard |
By original number of bytes |
< |
Little-endian |
Standard |
By original number of bytes |
> |
Big-endian |
Standard |
By original number of bytes |
! |
Network (= Big-endian) |
Standard |
By original number of bytes |
Format |
C Type |
Python Type | Standard
size |
Notes |
x |
Pad byte |
No value |
|
|
c |
char |
string of length 1 |
1 |
|
b |
signed char |
Integer |
1 |
(3) |
B |
unsigned char |
Integer |
1 |
(3) |
? |
_Bool |
bool |
1 |
(1) |
h |
short |
Integer |
2 |
(3) |
H |
unsigned short |
Integer |
2 |
(3) |
i |
int |
Integer |
4 |
(3) |
I |
unsigned int |
Integer |
4 |
(3) |
l |
long |
Integer |
4 |
(3) |
L |
unsigned long |
Integer |
4 |
(3) |
q |
long long |
Integer |
8 |
(2), (3) |
Q |
unsigned long long |
Integer |
8 |
(2), (3) |
f |
float |
Float |
4 |
(4) |
d |
double |
Float |
8 |
(4) |
s |
char[] |
String |
|
|
p |
char[] |
String |
|
|
P |
void * |
Integer |
|
(5), (3) |
4. Example
#Coding=utf-8" "struct implements read and write binary files" "ImportSYSImportstructdefWriteFile (path): Name= b"Zhanglin" Age= 30Sex= b"female"Profession= b"IT" Try: With open (path,"WB") as Pf:text= Struct.pack (">8si6s2s", Name,age,sex,profession) pf.write (text)Print("Write file success!") exceptException as E:Print("Write file faild!:", E)defReadFile (path): Text=NoneTry: With open (path,"RB") as Pf:text=Pf.read ()Print("Read File success!") Print(text)Print(Struct.unpack (">8si6s2s", text)) exceptException as E:Print("Read file faild!:", E)if __name__=="__main__": Path= Sys.argv[1] WriteFile (path) ReadFile (path)
Output Result:
>>> D:\pystu>python struct_test.py Struct_test.txt
>>> Write File success!
>>> Read File success!
>>> B ' Zhanglin\x00\x00\x00\x1efemaleit '
>>> (b ' Zhanglin ', b ' female ', B ' IT ')
The struct of Python