python csv reader ignore blank row

前端 未结 2 702
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 06:43

Im using the pythons csv reader . How can I use the following code in such a way that it ignores blank lines.

import csv
f1 = open (\"ted.csv\")
oldFile1 = c         


        
相关标签:
2条回答
  • 2020-12-20 07:16

    If your blanks are always on the first line, then Marius' answer is simplest. If you have n blanks at the beginning or you just want to skip some number of lines you can use itertools.islice().

    Skip first N lines

    Suppose you want to skip over the first 4 lines (blank lines or not):

    from itertools import islice
    with open('csv2.csv', 'r') as f1, open('out.csv', 'w') as out:
        filt_f1 = islice(f1, 4, None)
        r1 = csv.reader(filt_f1)
        wr = csv.writer(out)
        for line in r1:
            ...
    

    Blank lines throughout

    If you have blank lines scattered throughout your files then you can filter them out with itertools.filterfalse.

    import csv
    from itertools import filterfalse
    from itertools import chain
    
    with open('csv1.csv', 'r') as f1, open('csv2.csv', 'r') as f2, open('out.csv', 'w') as out:
        # create an iterator without lines that start with '\n'
        filt_f1 = filterfalse(lambda line: line.startswith('\n'), f1)
        filt_f2 = filterfalse(lambda line: line.startswith('\n'), f2)
    
        # csv.reader consumes the filtered iterators
        r1, r2 = csv.reader(filt_f1), csv.reader(filt_f2)
        wr = csv.writer(out)
    
        # here insert your logic, I just write both to the same file
        for line in chain(r1, r2):
            wr.writerow(line)
    

    Where csv1.csv is:

    time,name,location
    12345,Jean,Montreal
    
    12346,Peter,Chicago
    
    1234589,Doug,Boston
    

    and csv2.csv (note: not shown here, but csv2.csv has 4 blank lines at the top of the file):

    123457,Scott,San Diego
    
    123458,Jen,Miami
    
    123459,Robert,Sacramento
    

    output out.csv does not have blank lines throughout:

    time,name,location
    12345,Jean,Montreal
    12346,Peter,Chicago
    1234589,Doug,Boston
    123457,Scott,San Diego
    123458,Jen,Miami
    123459,Robert,Sacramento
    
    0 讨论(0)
  • 2020-12-20 07:30

    If your csv files start with a blank line, I think you should be able to skip that line with readline() before creating the csv reader:

    with open("ted.csv") as f1, open("ted2.csv") as f2, open('foo.csv', 'w') as out:
        f1.readline()
        f2.readline()
        r1, r2 = csv.reader(f1), csv.reader(f2)
    
    0 讨论(0)
提交回复
热议问题