3 回答
TA贡献1836条经验 获得超13个赞
创建自己的自定义过滤器功能,例如:
def skipper(fname):
with open(fname) as fin:
no_comments = (line for line in fin if not line.lstrip().startswith('#'))
next(no_comments, None) # skip header
for row in no_comments:
yield row
a = np.loadtxt(skipper('your_file'), delimiter=',')
TA贡献1848条经验 获得超10个赞
def skipper(fname, header=False):
with open(fname) as fin:
no_comments = (line for line in fin if not line.lstrip().startswith('#'))
if header:
next(no_comments, None) # skip header
for row in no_comments:
yield row
a = np.loadtxt(skipper('your_file'), delimiter=',')
鉴于在某些情况下,csv文件具有注释行(以#开头),但没有标题行,这只是对@Jon Clements答案的少许修改,增加了一个可选参数“ header”。
添加回答
举报