Python skips the first line to read the contents of a file

Source: Internet
Author: User

When programming python, it is often necessary to skip the first line to read the contents of the file. It is easier to think of setting a line_num for each row, and then determining whether Line_num is 1, and if not equal to 1, reads. The corresponding Python code is as follows:

[Python]View PlainCopy
    1. Input_file = open ("c:\\python34\\test.csv")
    2. Line_num = 0
    3. For line in Islice (Input_file, 1, None):
    4. Line_num + = 1
    5. if (line_num! = 1):
    6. Do_readline ()


However, the code execution is inefficient because it is necessary 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:

[Python]View PlainCopy
      1. From Itertools import islice
      2. Input_file = open ("c:\\python34\\test.csv")
      3. For line in Islice (Input_file, 1, None):
      4. Do_readline ()

Python skips the first line to read the contents of a file

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.