Python CSV reader to skip 9 headers

前端 未结 3 1576
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 07:57
import os
import csv

def get_file_path(filename):
    currentdirpath = os.getcwd()
    file_path = os.path.join(os.getcwd(), filename)
    print(file_path)
    return(f         


        
3条回答
  •  时光说笑
    2021-01-27 08:33

    You can use itertools.islice() to skip a fixed number of lines:

    from itertools import islice
    
    next(islice(reader, 9, 9), None)        
    for row in reader:
        print(row[0])                   
    

    The islice() object is instructed to skip 9 lines, then immediately stop without producing further results. It is itself an iterator, so you need to call next() on it still.

    If you wanted to skip rows until the 'empty' row, that requires a different approach. You'd have to inspect each row and stop reading when you come across one that has only empty cells:

    for row in reader:
        if not any(row):  # only empty cells or no cells at all
            break
    
    for row in reader:
        print(row[0])                   
    

    Demo of the latter approach:

    >>> import csv
    >>> import io
    >>> sample = '''\
    ... Summary Journal Entry,JE-00000060
    ... Journal Entry Date,28/02/2015
    ... Accounting Period,Feb-15
    ... Accounting Period Start,1/02/2015
    ... Accounting Period End,28/02/2015
    ... Included Transaction Types,Invoice Item
    ... Included Time Period,01/02/2015-09/02/2015
    ... Journal Run,JR-00000046
    ... Segments,
    ... ,
    ... Customer Account Number,Transaction Amount
    ... 210274174,545.45
    ... 210274174,909.09
    ... 210274174,909.09
    ... 210274174,909.09
    ... 210274174,909.09
    ... '''
    >>> with io.StringIO(sample) as csvfile:
    ...     reader = csv.reader(csvfile)
    ...     for row in reader:
    ...         if not [c for c in row if c]:
    ...             break
    ...     for row in reader:
    ...         print(row[0])                   
    ... 
    Customer Account Number
    210274174
    210274174
    210274174
    210274174
    210274174
    

    Note that you want to leave newline handling to the csv.reader; when opening your file set newline='':

    with open(filepath, 'r', newline='') as csvfile:
    

提交回复
热议问题