Using Python to read txt document method explained
Create a text document named record-txt in the G:/PythonPractise folder, write four lines as shown below and save.
Open python3's idle and start writing code.
The code and results of Method 1 are as follows:
As shown in the above running result, the above result is written omitting end=, which is equivalent to end="\n"(carriage return); The following result is written as end=""(empty string), equivalent to end="\r" (line feed)
The code and results of Method 2 are as follows:
Method 3 code results are as follows:
Compare the three methods, the first method returns the file under the path to an object, and then calls the readline() method of the object; Method three calls the readlines() method of the file, and method two walks through each line of the file and prints it line by line.
Note that this way of writing is wrong! :
This method calls the readline() method of the file object, the readline() method can only read one line of data, without a loop, can only read the first line of data in the file (general column header), so that it reads the content of the first line is only the content of the first line, and put the content of the first line into a list. I'm going to use a for in loop to go through the contents of this list, and I'm going to take out every character element in the string, and then I'm going to print those characters line by line, and that's vertical typesetting.
Attached: All escape characters and meanings:
The above article uses Python to read txt document method explanation is the small series to share with you all the content, I hope to give you a reference, but also hope that you support the script home.
Related article
-
How do I add logging to django
This article mainly introduces django to add log function, this article through the example code to give you a very detailed introduction, has a certain reference value, need friends can refer to2020-02-02 -
python uses matplotlib to customize the linear and markup types of drawings
This article mainly gives you a detailed introduction to python using matplotlib custom drawing lines, marking types, the article has detailed code examples, has a certain reference value, the need of friends can refer to2023-07-07 -
How to select the optimal threshold of ROC curve
Today, Xiaobian will share a talk about how to choose the best threshold of the ROC curve, which has a good reference value, and I hope it will be helpful to you. Let's take a look2020-02-02 -
How does Python remove unwanted characters from a string
This article mainly introduces Python how to remove unnecessary characters in the string problem, has a good reference value, I hope to help you, if there are mistakes or not considered completely, please advise2023-08-08 -
Detail the difference between using jsonify and json.dumps in flask
The following Xiaobian will share a detailed discussion of the difference between using jsonify and json.dumps in flask, which has a good reference value, and I hope to help you. Let's take a look2018-03-03 -
Method steps for packaging python programs with setuptools
This article mainly introduces the use of setuptools packaging python program method steps, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need for friends to learn together with the small series2020-01-01 -
How to import PySpark into Python's implementation (2)
This article mainly introduces how to import PySpark into Python put implementation, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, the need for friends to learn together with Xiaobian2020-04-04 -
Why auto_now is not updated during django model update and how to resolve it
This article mainly introduces the reasons and solutions for auto_now not being updated when django model is updated. It has a good reference value and I hope it will be helpful to you. Let's take a look2020-04-04 -
pandas implementation of filtering data by date range
This article mainly introduces the implementation of pandas by date range screening data, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, need friends to follow the Xiaobian to learn together2021-02-02 -
python scatter draws scatter plots
This article mainly introduces python scatter map drawing, the article around the theme to launch a detailed content introduction, has a certain reference value, the need of small partners can refer to it2022-06-06
Latest comments