[Python] read () readline () readlines () comparison

Python built-in functions to read and write the file, or the underlying implementation C.

 

Reading file functions structure:

Read the file mode to use to open a file object, using Python's built-in open () function, passing the file name and identifier.

"R" indicates a read file, if the file does not exist, it will throw an error code and detailed information.

If the file is opened successfully, you can use to read) function to read the entire contents of a file to read () function, for example, Python read the contents of memory, expressed as a str object.

After reading the end of the call the close () method closes.

f = open('/Users/michael/test.txt', 'r')
f.close()

Because the file may be generated when the read error, the error once, later f.close () will not be called, so in order to ensure the normal closing files, may be used try ... finally exception handling.

try:
    f = open('/path/to/file', 'r')
     f.read()
finally:
    if f:
        f.close()

You can also use a simpler method with (). This function does not need to call close (). This function reads off automatically after the end of the structure.

with open('/path/to/file', 'r') as f:
     line = f.read()
    ...

Read mode:

Python read mode, a total of three, namely, read (), readline (), readlines ()

read():

Call read () will read the entire contents of a file one time, the fastest, but does not apply when the data is too large.

readline():

Using the readline () method, the content read line by line, the result is returned list.

Open with ( ' / path / to / File ' , ' R & lt ' ) AS F: 
    Line = f.readline ()
     the while Line: 
        Line = f.readine ()   # where only read one line data 
        ...

readlines():

Choosing the readlines () method, a one-time read all the contents of the text, the result is returned List, wherein the end of each element has "\ n". This method is suitable to read the profile, faster, but the larger the text occupies more memory.

This method of reading text, each line of text will end with a '\ n' newline (use L.rstrip ( '\ n') to remove the newline). Note that calling the python method returns a copy of the original elements will not have to be modified, if you want to modify the end of the symbol, need to be addressed assigned to the new string.

with open('/path/to/file', 'r') as f:
    line = f.readlines()
 
 
for i in line:
    lines.append(i.rstrip('\n'))

Optimization of treatment:

Usually in data mining, data is orderly. Therefore, using an iterative way to read data is a good balance between time and memory pythonic wording:

split () delimiter string specified by slicing, if the parameter value is specified num, num substrings separated only by split () method so that the read () Returns a string. This is the most common approach.

However, this approach has strict requirements for the format. For example, if the end of the time there are multiple "\ n", then the last few List elements may be empty.

with open('/path/to/file','r') as f:
   line = f.read().split('\n')

 

Note: The contents of the file to cover the additional problem:

A file is not closed before the object can be written many times, and each time the end of the additional content in the last, but that's not really written into the document, and the content on the memory, all at once after the cursor is closed write.

When the cursor is closed and then open the file, it will start writing the first line of the file, overwriting the original content.

 

Guess you like

Origin www.cnblogs.com/guangluwutu/p/11958415.html