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

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

    In a similar use case I had to skip annoying lines before the line with my actual column names. This solution worked nicely. Read the file first, then pass the list to csv.DictReader.

    with open('all16.csv') as tmp:
        # Skip first line (if any)
        next(tmp, None)
    
        # {line_num: row}
        data = dict(enumerate(csv.DictReader(tmp)))
    
    0 讨论(0)
  • 2020-11-22 10:20

    just add [1:]

    example below:

    data = pd.read_csv("/Users/xyz/Desktop/xyxData/xyz.csv", sep=',', header=None)**[1:]**
    

    that works for me in iPython

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

    I would use tail to get rid of the unwanted first line:

    tail -n +2 $INFIL | whatever_script.py 
    
    0 讨论(0)
  • 2020-11-22 10:25

    You would normally use next(incsv) which advances the iterator one row, so you skip the header. The other (say you wanted to skip 30 rows) would be:

    from itertools import islice
    for row in islice(incsv, 30, None):
        # process
    
    0 讨论(0)
  • 2020-11-22 10:25

    I would convert csvreader to list, then pop the first element

    import csv        
    
    with open(fileName, 'r') as csvfile:
            csvreader = csv.reader(csvfile)
            data = list(csvreader)               # Convert to list
            data.pop(0)                          # Removes the first row
    
            for row in data:
                print(row)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题