Understanding the meaning of rb in python

Several modes of Python file reading and writing:

r,rb,w,wb So when reading and writing files, what is the main difference between whether there is a b mark?

File usage identification

When reading a file and reading a file, until the end of document (EOF) is read, the end of the file will not be counted. Python will consider the character converted from the byte \x1A(26) as the end of document (EOF).

Therefore, when using'r' to read binary files, incomplete document reading may occur.

Example:

The binary file has the following data arranged from low to high: 7F 32 1A 2F 3D 2C 12 2E 76

If you use'r' to read, the third byte is read, that is, the end of the file is considered.

If you use'rb' to read according to binary bits, the read bytes will not be converted into characters, thus avoiding the above error.

solution:

Binary files use the binary method to read'rb'

to sum up:

When using'r', if it encounters '0x1A', it is regarded as the end of the file, which is EOF. There is no such problem when using'rb'.

That is: if you write in binary and then read out from a file, if there is '0x1A' in it, only a part of the file will be read, and using'rb' will always read the end of the file.

Knowledge point expansion:

  1. r is to read manually written data, what it looks like when it is written, and what it looks like when it is read.
  2. rb is to read binary files, non-manually written data such as .jpeg and so on.
  3. In addition, encoding can specify the decoding format. utf-8 is the current mainstream encoding method. If encoding is not specified, there will be different decoding methods depending on the python version. Python3 is utf-8, and python2 is ascii.

So far, this article on the understanding of the meaning of rb in python is introduced. For more relevant content of what rb in python means, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. Cn!

Recommended Posts

Understanding the meaning of rb in python
The meaning and usage of lists in python
The usage of wheel in python
The usage of tuples in python
The usage of Ajax in Python3 crawler
What is the function of adb in python
Python implements the shuffling of the cards in Doudizhu
The consequences of uninstalling python in ubuntu, very
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
How to understand the introduction of packages in Python
Deep understanding of Python multithreading
Consolidate the foundation of Python (3)
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
Summary of logarithm method in Python
Use of Pandas in Python development
In-depth understanding of Python variable scope
Python handles the 4 wheels of Chinese
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Detailed usage of dictionary in Python
Usage of os package in python
​What are the numbers in Python?
Talking about the modules in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
End the method of running python
Description of in parameterization in python mysql
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Implementation of JWT user authentication in python
Logistic regression at the bottom of python
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Analysis of glob in python standard library
Where is the pip path of python3
Method of installing django module in python
The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
Knowledge points of shell execution in python
The specific method of python import library
Solve the conflict of multiple versions of python
Python randomly shuffles the elements in the list
Detailed explanation of the use of pip in Python | summary of third-party library installation