打印出第一行以外的csv行

时间:2014-11-11 15:06:53

标签: python csv

我想用Python读取一个CSV文件,然后打印出除第一行之外的每一行。

我知道如何打印出所有行:

with open('myfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        print row

我认为不打印第一行的唯一方法是:

with open('myfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for i, row in enumerate(reader):
        if i != 0:
            print row

但这看起来并不优雅。还有其他解决方案吗?

1 个答案:

答案 0 :(得分:6)

csv阅读器对象是迭代器,这意味着您可以使用next()跳过单个条目:

with open('myfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader) # just ignore the result
    for row in reader:
        print row