File reading methods read(), readline(), readlines() in Python

The difference between the file reading methods read(), readline() and readlines() in Python:

    1. read() method

       >>> file = open('filename') # filename: filename

       >>> type(file.read())

       <type 'str'> # that is, the string form of all the contents of the file is obtained

   2. readline() method

       >>> file = open('filename')

       >>> type(file.readline())

       <type 'str'>             

       Note that what you get at this time is still in string format, but what you get is the string form of the first line of data in the file. If you use it again, you get the second line

The string format of the data, and so on.

    3. readlines() method     

       >>> file = open('filename')

       >>> type(file.readlines())

       <type 'list'>

       At this point, what we get is a list structure, and the elements of the list are strings. Each element in the list is in string format for each line of data in the file.

The above methods all contain invisible characters.

      In addition, the strip() method is often used to delete invisible characters, and the split() method is used to split the string into words. The split() defaults to a space as the splitter.

Use a custom character as a splitter.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324488202&siteId=291194637