Python provides a simple implementation method for reading files by row,

Source: Internet
Author: User

Python provides a simple implementation method for reading files by row,

1: readline ()

File = open ("sample.txt") while 1: line = file. readline () if not line: break pass # do somethingfile. close ()

Reading data from a file in one row is obviously slow;

However, it saves a lot of memory;

The test reads 10 million sample.txt files and reads about 32000 lines per second;

2: fileinput

Import fileinput for line in fileinput. input ("sample.txt"): pass

The writing method is simpler. However, after the test, we found that only 13000 rows of data can be read per second, which is twice as slow as the previous method;

3: readlines ()

File = open ("sample.txt") while 1: lines = file. readlines (100000) if not lines: break for line in lines: pass # do somethingfile. close ()

With the same data test, it can read 96900 rows of data per second! The efficiency is three times that of the first method, and seven times that of the second method!

4: file iterator

Read and display only one row at a time. When reading large files, it should be as follows:

File = open ("sample.txt") for line in file: pass # do somethingfile. close ()

The above is a simple implementation of Python reading files by line. I hope you can provide more support ~

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.