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

前端 未结 17 1940
庸人自扰
庸人自扰 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:32

    Borrowed from python cookbook,
    A more concise template code might look like this:

    import csv
    with open('stocks.csv') as f:
        f_csv = csv.reader(f) 
        headers = next(f_csv) 
        for row in f_csv:
            # Process row ...
    
    0 讨论(0)
  • 2020-11-22 10:32

    Python 2.x

    csvreader.next()

    Return the next row of the reader’s iterable object as a list, parsed according to the current dialect.

    csv_data = csv.reader(open('sample.csv'))
    csv_data.next() # skip first row
    for row in csv_data:
        print(row) # should print second row
    

    Python 3.x

    csvreader.__next__()

    Return the next row of the reader’s iterable object as a list (if the object was returned from reader()) or a dict (if it is a DictReader instance), parsed according to the current dialect. Usually you should call this as next(reader).

    csv_data = csv.reader(open('sample.csv'))
    csv_data.__next__() # skip first row
    for row in csv_data:
        print(row) # should print second row
    
    0 讨论(0)
  • 2020-11-22 10:32

    Python 3.X

    Handles UTF8 BOM + HEADER

    It was quite frustrating that the csv module could not easily get the header, there is also a bug with the UTF-8 BOM (first char in file). This works for me using only the csv module:

    import csv
    
    def read_csv(self, csv_path, delimiter):
        with open(csv_path, newline='', encoding='utf-8') as f:
            # https://bugs.python.org/issue7185
            # Remove UTF8 BOM.
            txt = f.read()[1:]
    
        # Remove header line.
        header = txt.splitlines()[:1]
        lines = txt.splitlines()[1:]
    
        # Convert to list.
        csv_rows = list(csv.reader(lines, delimiter=delimiter))
    
        for row in csv_rows:
            value = row[INDEX_HERE]
    
    0 讨论(0)
  • 2020-11-22 10:34

    To skip the first line just call:

    next(inf)
    

    Files in Python are iterators over lines.

    0 讨论(0)
  • 2020-11-22 10:35

    this might be a very old question but with pandas we have a very easy solution

    import pandas as pd
    
    data=pd.read_csv('all16.csv',skiprows=1)
    data['column'].min()
    

    with skiprows=1 we can skip the first row then we can find the least value using data['column'].min()

    0 讨论(0)
  • 2020-11-22 10:36

    The documentation for the Python 3 CSV module provides this example:

    with open('example.csv', newline='') as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(1024))
        csvfile.seek(0)
        reader = csv.reader(csvfile, dialect)
        # ... process CSV file contents here ...
    

    The Sniffer will try to auto-detect many things about the CSV file. You need to explicitly call its has_header() method to determine whether the file has a header line. If it does, then skip the first row when iterating the CSV rows. You can do it like this:

    if sniffer.has_header():
        for header_row in reader:
            break
    for data_row in reader:
        # do something with the row
    
    0 讨论(0)
提交回复
热议问题