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
- Input_file = open ("c:\\python34\\test.csv")
- Line_num = 0
- For line in Islice (Input_file, 1, None):
- Line_num + = 1
- if (line_num! = 1):
- 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
- From Itertools import islice
- Input_file = open ("c:\\python34\\test.csv")
- For line in Islice (Input_file, 1, None):
- Do_readline ()
Python skips the first line to read the contents of a file