File operations in Python in a small note

Source: Internet
Author: User
Tags unpack

Simple to open the head, recently in the study of Python, want to make a simple note, so wrote this article

First of all, say that there are many modules in Python, divided into the system and the external, here I will briefly introduce about the binary file

Conversion and storage, then the package used today is "struct", first introduce the "struct" inside two more useful things, one is

"Struct.pack (" I ", a)", this is the conversion of a decimal number into a binary, where the parameters I explain in the back. Another more useful

Is "Struct.unpack (" I ", bytesint)", needless to say, this is to turn the binary into decimal, then I will use the Python code

Introduce the use of these two functions and some other small knowledge

1.struct.pack

Import struct #导入struct模块

a,b,c=12,10,5

Bytesint=struct.pack ("III", A,b,c) #其中i是int类型的意思, several integers, corresponding to several I

Print (Bytesint) #打印出后 (an int, accounting for four bytes) = "

B ' \x0c\x00\x00\x00\n\x00\x00\x00\x05\x00\x00\x00 '

File=open ("BytesFile.txt", "WB") #输入一个二进制文本, BytesFile.txt is the name of the text,

WB is binary text write, RB is binary text read in

File.write (bytesint) #把bytesInt write bytesFile.txt inside

File.close () #关闭文件

#好了, the above is the simple application of "Struct.pack". Other types I think everyone will extrapolate.

2.struct.unpack

Import struct

File=open ("BytesFile.txt", "RB") #我在这里打开一个二进制的文本,

I'm going to put the bytesint in the BytesFile.txt.

Datas=file.read (4) #我在这里读file because int accounts for four bytes, so read (4),

Data=struct.unpack ("I", datas) [0] #里面参数与上面一样, but much explained, where the bytes actually

can also be viewed as a list, so I take the first byte [0]

Print (data) #打印出后 = "12

File.close () #每一次打开文件, remember to turn it off OH

3.bytes,encode, decode

S= "World"

L=s.encode ("Utf-8") utf-8 encoding s for #以utf-8 format

Print (l) # after printing = "B ' \xe4\xb8\x96\xe7\x95\x8c '

Print (L.decode ("Utf-8")) #打印出后 decoding = "World

Print (bytes (s, "Utf-8")) #打印出后 turn str into byte format, with encode= effect

B ' \xe4\xb8\x96\xe7\x95\x8c '

The first attribute value of 4.pack (transferred from official: https://docs.python.org/3/library/struct.html)

Standard
Format C Type Python Typesize Notes
x Pad byte No value
c char bytes of length 1 1
b signed char Integer 1 (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)
n ssize_t Integer (4)
N size_t Integer (4)
e (7) Float 2 (5)
f float Float 4 (5)
d double Float 8 (5)
s char[] bytes
p char[] bytes
P void * Integer (6)

  

OK, it's here today, there are updates in the back oh (if there are errors, please correct, thank you OH):)

                

    

File operations in Python in a small note

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.