Python 使用 CSV 文件跳过循环中的第一行(字段)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14674275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Skip first line(field) in loop using CSV file?
提问by Raitis Kupce
Possible Duplicate:When processing CSV data, how do I ignore the first line of data?
I am using python to open CSV file. I am using formula loop but I need to skip the first row because it has header.
我正在使用 python 打开 CSV 文件。我正在使用公式循环,但我需要跳过第一行,因为它有标题。
So far I remember was something like this but it is missing something: I wonder if someone knows the code for what I am trying to do.
到目前为止,我记得是这样的,但它缺少一些东西:我想知道是否有人知道我想要做的事情的代码。
for row in kidfile:
if row.firstline = false: # <====== Something is missing here.
continue
if ......
采纳答案by Bakuriu
Probably you want something like:
可能你想要这样的东西:
firstline = True
for row in kidfile:
if firstline: #skip first line
firstline = False
continue
# parse the line
An other way to achive the same result is calling readlinebefore the loop:
获得相同结果的另一种方法是readline在循环之前调用:
kidfile.readline() # skip the first line
for row in kidfile:
#parse the line
回答by Andrea
There are many ways to skip the first line. In addition to those said by Bakuriu, I would add:
有很多方法可以跳过第一行。除了 Bakuriu 所说的那些,我还要补充:
with open(filename, 'r') as f:
next(f)
for line in f:
and:
和:
with open(filename,'r') as f:
lines = f.readlines()[1:]
回答by user2037553
csvreader.next() Return the next row of the reader's iterable object as a list, parsed according to the current dialect.
csvreader.next() 以列表形式返回阅读器可迭代对象的下一行,根据当前方言进行解析。

