How to ignore the first line of data when processing CSV data?

前端 未结 17 1937
庸人自扰
庸人自扰 2020-11-22 10:05

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

17条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 10:26

    Because this is related to something I was doing, I'll share here.

    What if we're not sure if there's a header and you also don't feel like importing sniffer and other things?

    If your task is basic, such as printing or appending to a list or array, you could just use an if statement:

    # Let's say there's 4 columns
    with open('file.csv') as csvfile:
         csvreader = csv.reader(csvfile)
    # read first line
         first_line = next(csvreader)
    # My headers were just text. You can use any suitable conditional here
         if len(first_line) == 4:
              array.append(first_line)
    # Now we'll just iterate over everything else as usual:
         for row in csvreader:
              array.append(row)
    

提交回复
热议问题