处理CSV数据时,如何忽略第一行数据? [英] When processing CSV data, how do I ignore the first line of data?

查看:2141
本文介绍了处理CSV数据时,如何忽略第一行数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求Python从CSV数据列中打印最小数字,但顶行是列数,我不想让Python占据顶行。如何确保Python忽略第一行?

I am asking Python to print the minimum number from a column of CSV data, but the top row is the column number, and I don't want Python to take the top row into account. How can I make sure Python ignores the first line?

这是到目前为止的代码:

This is the code so far:

import csv

with open('all16.csv', 'rb') as inf:
    incsv = csv.reader(inf)
    column = 1                
    datatype = float          
    data = (datatype(column) for row in incsv)   
    least_value = min(data)

print least_value

你能解释你在做什么,而不仅仅是给出代码?我非常新的Python,并希望确保我理解一切。

Could you also explain what you are doing, not just give the code? I am very very new to Python and would like to make sure I understand everything.

推荐答案

您可以使用 csv 模组的 Sniffer 以检测是否存在标题行和内置的 next()函数如果需要,跳过它:

You could use thecsvmodule's Sniffer class to detect whether a header row is present and the built-innext()function to skip over it if necessary:

import csv

with open('all16.csv', 'rb') as inf:
    has_header = csv.Sniffer().has_header(inf.read(1024))
    inf.seek(0)  # rewind
    incsv = csv.reader(inf)
    if has_header:
        next(incsv)  # skip header row
    column = 1
    datatype = float
    data = (datatype(row[column]) for row in incsv)
    least_value = min(data)

print least_value

如果您使用的是Python 2.x,您也可以跳过 incsv.next()调用的行。

You can also skip rows with a incsv.next() call if you're using Python 2.x.

由于数据类型在您的示例中是硬编码的,因此读取< c> data 这样:

Since datatype and column are hardcoded in your example, it would be slightly faster to read data this way:

    data = (float(row[1]) for row in incsv)

这篇关于处理CSV数据时,如何忽略第一行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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