Python skips the first few lines to read the contents of the file __python

Source: Internet
Author: User

In Python programming, you often need to skip the first line to read the contents of a file. It is easy to think of setting a line_num for each line, and then determining if Line_num is 1, and if not equal to 1, read the operation. The corresponding Python code is as follows:

Input_file = open ("Test.csv")  
line_num = 0  
for line in Islice (Input_file, 1, None):  
    line_num + = 1  
    if (line _num!= 1):  
        do_readline ()  
However, this write code is inefficient because each time you need to determine whether the current line number is 1. Using the Itertools tools provided by Python, we can avoid such problems. The purpose of Itertools is to raise the efficiency of looping. The corresponding code is as follows:

From itertools import islice  
input_file = open ("Test.csv") for line in  
islice (input_file, 1, None):  
    



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.