Home > Article > Backend Development > How to read txt file content in python
How to read txt files in python: 1. Use the read() function to read the contents of the txt file byte by byte or character; 2. Use the readline() function to read the contents of the txt file line by line. ;3. Use the readlines() function to read multiple lines of content in the txt file at one time.
The operating environment of this tutorial: windows7 system, python3.7 version, DELL G3 computer
Python provides the following 3 functions, they are It can help us realize the operation of reading data in the file:
read() function: read the contents of the file byte or character by byte;
readline() function: read the contents of the file line by line;
readlines() function: read the contents of multiple lines of the file at once.
Python read() function
For the open() function, and in readable mode (including r , r, rb, rb), you can call the read() function to read the contents of the file byte by byte (or character by character).
If the file is opened in text mode (non-binary mode), the read() function will read character by character; conversely, if the file is opened in binary mode, the read() function will read byte by byte to read.
The basic syntax format of the read() function is as follows:
file.read([size])
Among them, file represents the opened file object; size is an optional parameter used to specify the maximum number of characters that can be read at one time (bytes) number, if omitted, the default is to read everything at once.
For example, first create a text file named my_file.txt, whose content is:
Python教程 //m.sbmmt.com/course/list/30.html
Then create a file.py file in the same directory as my_file.txt, And write the following statement:
#以 utf-8 的编码格式打开指定文件 f = open("my_file.txt",encoding = "utf-8") #输出读取到的数据 print(f.read()) #关闭文件 f.close()
The program execution result is:
Python教程 //m.sbmmt.com/course/list/30.html
Note that when the file operation is completed, the close() function must be called to manually close the open file, so that It can avoid unnecessary errors in the program.
Of course, we can also specify the maximum number of characters (or bytes) that read() can read each time by using the size parameter, for example:
#以 utf-8 的编码格式打开指定文件 f = open("my_file.txt",encoding = "utf-8") #输出读取到的数据 print(f.read(6)) #关闭文件 f.close()
Program execution results For:
Python
Obviously, the read() function in this program only reads the first 6 characters of the my_file file.
Again, size represents the maximum number of characters (or bytes) that can be read at one time. Therefore, even if the set size is greater than the number of characters (bytes) stored in the file, the read() function will not No error will be reported, it will only read all the data in the file.
In addition, for files opened in binary format, the read() function will read the contents of the file byte by byte. For example:
#以二进制形式打开指定文件 f = open("my_file.txt",'rb+') #输出读取到的数据 print(f.read()) #关闭文件 f.close()
The program execution result is:
b'Python\xe6\x95\x99\xe7\xa8\x8b\r\n//m.sbmmt.com/course/list/30.html'
You can see that the output data is a bytes byte string. We can call the decode() method to convert it into a string we recognize.
Python readline() function
readline() function is used to read a line in the file, including the final newline character "\n ". The basic syntax format of this function is:
file.readline([size])
where file is the open file object; size is an optional parameter, used to specify the maximum number of characters (bytes) read at one time when reading each line. .
Same as the read() function, the prerequisite for this function to successfully read file data is that the mode specified by the open() function to open the file must be readable mode (including r, rb, r, rb) ).
The following program demonstrates the specific usage of the readline() function:
f = open("my_file.txt") #读取一行数据 byt = f.readline() print(byt)
The execution result of the program is:
Python教程
Because the readline() function reads the content of a line in the file , the last newline character "\n" will be read, and the print() function will wrap the content by default when outputting the content, so you will see an extra blank line in the output result.
Not only that, when reading line by line, you can also limit the maximum number of characters (bytes) that can be read, for example:
#以二进制形式打开指定文件 f = open("my_file.txt",'rb') byt = f.readline(6) print(byt)
The running result is:
b'Python'
Compared with the output of the previous example, since one line of data is not completely read here, the newline character will not be read.
Python readlines() function
readlines() function is used to read all lines in the file. It and the call do not specify the size parameter. The read() function is similar, except that the function returns a list of strings, where each element is a line of content in the file.
Like the readline() function, the readlines() function reads each line along with the newline character at the end of the line.
The basic syntax format of the readlines() function is as follows:
file.readlines()
Among them, file is the open file object. Like the read() and readline() functions, it requires that the mode of opening the file must be readable mode (including r, rb, r, rb).
For example:
f = open("my_file.txt",'rb') byt = f.readlines() print(byt)
The running result is:
[b'Python\xbd\xcc\xb3\xcc\r\n', b'//m.sbmmt.com/course/list/30.html']
[Related recommendations: Python3 video tutorial]
The above is the detailed content of How to read txt file content in python. For more information, please follow other related articles on the PHP Chinese website!