Python: skip comment lines marked with # in csv.DictReader

前端 未结 4 1612
旧时难觅i
旧时难觅i 2020-12-01 01:35

Processing CSV files with csv.DictReader is great - but I have CSV files with comment lines in (indicated by a hash at the start of a line), for example:

# step s         


        
4条回答
  •  感情败类
    2020-12-01 02:12

    Good question, and a good example of how Python's CSV library lacks important functionality, such as handling basic comments (not uncommon at the top of CSV files). While Dan Stowell's solution works for the specific case of the OP, it is limited in that # must appear as the first symbol. A more generic solution would be:

    def decomment(csvfile):
        for row in csvfile:
            raw = row.split('#')[0].strip()
            if raw: yield raw
    
    with open('dummy.csv') as csvfile:
        reader = csv.reader(decomment(csvfile))
        for row in reader:
            print(row)
    

    As an example, the following dummy.csv file:

    # comment
     # comment
    a,b,c # comment
    1,2,3
    10,20,30
    # comment
    

    returns

    ['a', 'b', 'c']
    ['1', '2', '3']
    ['10', '20', '30']
    

    Of course, this works just as well with csv.DictReader().

提交回复
热议问题