跳过循环中的第一行(字段)使用CSV文件? [英] Skip first line(field) in loop using CSV file?

查看:217
本文介绍了跳过循环中的第一行(字段)使用CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我使用python打开CSV文件。我正在使用公式循环,但我需要跳过第一行,因为它有头。

到目前为止,我记得是这样的,但它是缺少的东西:我不知道是否

 对于kidfile中的行:
if row.firstline = false:#< ======这里缺少一些东西。
continue
if ......


解决方案可能你想要的东西是:

  firstline = True 
用于kidfile中的行:
if firstline:#skip first line
firstline = False
continue
#解析行

获得相同结果的另一种方法是在循环之前调用 readline

 kidfile.readline()#跳过第一行
作为kidfile中的行:
#parse the line


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.

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 ......

解决方案

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 readline before the loop:

kidfile.readline()   # skip the first line
for row in kidfile:
    #parse the line

这篇关于跳过循环中的第一行(字段)使用CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆