Python csv 跳过第一行

Python处理csv文件时,经常需要跳过第一行表头读取文件内容。下面是正常读取的代码。

import csv
with open('表格/2019-04-01.csv', 'r') as read_file:
    reader = csv.reader(read_file)
    for row in reader:
        print(row)

如果需要跳过第一行,可以每次判断行数是否为1。但这样写的代码执行效率偏低,因为每次都需要判断当前的行号。

使用Python提供的itertools工具,我们可以避免此类问题。itertools的目的就是为了提搞looping的效率。

修改后的代码如下:

import csv
from itertools import islice
with open('表格/2019-04-01.csv', 'r') as read_file:
    reader = csv.reader(read_file)
    for row in islice(reader, 1, None):
        print(row)

猜你喜欢

转载自blog.csdn.net/qq_34626094/article/details/112919638