In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python how to read the contents of text files, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1 Open the file
Text operation can be thought of as adding water and draining water to the pool. A text file is like a pool for storing water, and the data is like water. Reading data from a text file is like letting a pool drain. In the process, we need a "pipeline" to read from the data. In the Python language, the open () function is such a "pipeline". When the open () function successfully opens the file, we get a file object.
File = open ('One Day.txt', 'r')
However, operating files often have a variety of exceptions, such as the file does not exist, the file does not have readable attributes and so on. Therefore, we need to do exception handling. The with statement is recommended here, and the logic related to exception handling has been implemented internally. Another benefit is that we can close the file without calling the close () function.
With open ('One Day.txt', 'r') as file:
Pass
The second argument to the open () function is the open mode. It can be read-only r, write w, append a, read rb in binary form, and so on.
2 read ()
The read () function reads data in a somewhat violent way. It reads all the contents of the file into memory at once. If there are too many files, the memory will burst. To be on the safe side, we usually read only a short section at a time and then call it repeatedly.
#-*-coding:utf-8-*-
Size = 1024
With open ('one day.txt', 'r') as file:
Print (file.read (size))
3 readline ()
If we need to read only one line at a time, we need to use the readline () function. Although this kind of reading method is not efficient, it takes up a small amount of memory and can be read and used.
#-*-coding:utf-8-*-
Import time
With open ('one day.txt', 'r') as file:
For line in file:
# realine () reads the entire line, including the "\ n" character
Print (file.readline () .strip ())
Time.sleep (1)
3 readlines ()
The realines () function, like read (), reads everything at once and returns a list object by row. This way of reading will be faster. But as the text grows, it takes up more and more memory. Configuration files are generally read, and you can use this method.
#-*-coding:utf-8-*-
Import time
With open ('one day.txt', 'r') as file:
For line in file.readlines ():
Print (line.strip ())
Time.sleep (1)
After reading the above, have you mastered how to read the contents of a text file in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.