python3程序运行中会跳过注释行_Python:跳过在csv.DictReader中用#标记的注释行

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 size=1.61853

val0,val1,val2,hybridisation,temp,smattr

0.206895,0.797923,0.202077,0.631199,0.368801,0.311052,0.688948,0.597237,0.402763

-169.32,1,1.61853,2.04069e-92,1,0.000906546,0.999093,0.241356,0.758644,0.202382

# adaptation finished

I could easily do something hacky, but I imagine there's a nice way to wrap a csv.DicReader around some other iterator object, which preprocesses to discard the lines.

解决方案

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值