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

前端 未结 4 1603
旧时难觅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:29

    Actually this works nicely with filter:

    import csv
    fp = open('samples.csv')
    rdr = csv.DictReader(filter(lambda row: row[0]!='#', fp))
    for row in rdr:
        print(row)
    fp.close()
    

提交回复
热议问题