Using Python to read txt document method explain _python_ script home

Using Python to read txt document method explained

Updated: Jun 23, 2018 15:49:21 by SuperGiser_Lee
Today, Xiaobian will share a method of using Python to read txt documents for you, which has a good reference value, and I hope to help you. Let's take a look

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

  • 如何在django中添加日志功能

    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 to
    2020-02-02
  • python使用matplotlib定制绘图的线型、标记类型

    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 to
    2023-07-07
  • 浅谈ROC曲线的最佳阈值如何选取

    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 look
    2020-02-02
  • Python如何去除字符串中不需要的字符

    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 advise
    2023-08-08
  • 详谈在flask中使用jsonify和json.dumps的区别

    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 look
    2018-03-03
  • 利用setuptools打包python程序的方法步骤

    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 series
    2020-01-01
  • 如何将PySpark导入Python的放实现(2种)

    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 Xiaobian
    2020-04-04
  • django model的update时auto_now不被更新的原因及解决方式

    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 look
    2020-04-04
  • pandas 按日期范围筛选数据的实现

    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 together
    2021-02-02
  • python scatter绘制散点图

    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 it
    2022-06-06

Latest comments