Three reads read () in Python, ReadLine () and ReadLines () __python

Source: Internet
Author: User
Tags readline
When we talk about "text processing", we usually refer to what we are dealing with. Python is easy to read the contents of a text file into a string variable that can be manipulated. The file object provides three read methods:. Read (),. ReadLine (), and. ReadLines (). Each method can accept a variable to limit the amount of data read at a time, but they usually do not use a variable. read () reads the entire file at a time, and is typically used to place the contents of the file in a string variable. However, the. Read () generates the most direct string representation of a file's content, but it is unnecessary for sequential row-oriented processing, and is not possible if the file is larger than available memory.

. ReadLine () and. ReadLines () are very similar. They are all used in structures similar to the following:

Python. ReadLines () example

FH = open (' C:\\autoexec.bat ') for line in Fh.readlines (): Print Line.readline () and. ReadLine The difference between S () is that the latter reads the entire file at once, like. Read (). ReadLines () automatically analyzes the contents of a file into a list of rows, which can be made up of Python's for ... Structure for processing. On the other hand,. ReadLine () reads only one row at a time, usually much slower than. ReadLines (). You should use the. ReadLine () only if there is not enough memory to read the entire file at once.

Write:

WriteLine () is the output after the line, the next write will be written on the next line. Write () is the output after the cursor at the end of line will not wrap, next write will be followed by this line



With ReadLine output, this footprint is relatively small for larger files.
#coding: utf-8

f = open (' Poem.txt ', ' r ') result
= list () for line in
open (' Poem.txt '): Line
    = F.readline ()
    Print line
    result.append (line)
print result
f.close ()                
open (' Result-readline.txt ', ' W '). Write ('%s '% \ n '. Join (Result))



#coding: Utf-8
' cdays-4-exercise-6.py file basic Operation
    @note: File read Write, list sort, string operation
    @see: String Each method can refer to HEKP (str) or Python online documentation http://docs.python.org/lib/string-methods.html
' '

f = open (' Cdays-4-test.txt ', ' R ')                   # Open file as read result
= list () with line in
f.readlines ():                          #依次读取每行 Line
    = Line.strip ()                             #去掉每行头尾空白
    If not Len (line) or Line.startswith (' # '):       #判断是否是空行或注释行
        continue                                    #是的话, skipping unhandled
    Result.append (line)                             #保存
result.sort ()                                       #排序结果
Print result
open (' Cdays-4-result.txt ', ' W '). Write ('%s '% '. Join (Result)) #保存入结果文件



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.