pandas.read_csv:如何跳过注释行 [英] pandas.read_csv: how to skip comment lines

查看:100
本文介绍了pandas.read_csv:如何跳过注释行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我误会了read_csv的意图.如果我有文件"j"之类的

I think I misunderstand the intention of read_csv. If I have a file 'j' like

# notes
a,b,c
# more notes
1,2,3

我如何pandas.read_csv这个文件,跳过任何'#'注释行?我在帮助行的注释中看到它不被支持,但是它指示应该返回一个空行.我看到一个错误

How can I pandas.read_csv this file, skipping any '#' commented lines? I see in the help 'comment' of lines is not supported but it indicates an empty line should be returned. I see an error

df = pandas.read_csv('j', comment='#')

CParserError:标记数据时出错. C错误:第2行中应有1个字段,看到了3

CParserError: Error tokenizing data. C error: Expected 1 fields in line 2, saw 3

我目前在

In [15]: pandas.__version__
Out[15]: '0.12.0rc1'

在版本'0.12.0-199-g4c8ad82'上:

On version'0.12.0-199-g4c8ad82':

In [43]: df = pandas.read_csv('j', comment='#', header=None)

CParserError:标记数据时出错. C错误:第2行中应有1个字段,看到了3

CParserError: Error tokenizing data. C error: Expected 1 fields in line 2, saw 3

推荐答案

所以我相信在最新版本的pandas(版本0.16.0)中,您可以将comment='#'参数放入pd.read_csv中,这应该跳过注释掉行.

So I believe in the latest releases of pandas (version 0.16.0), you could throw in the comment='#' parameter into pd.read_csv and this should skip commented out lines.

这些github问题表明您可以执行以下操作:

These github issues shows that you can do this:

  • https://github.com/pydata/pandas/issues/10548
  • https://github.com/pydata/pandas/issues/4623

请参阅read_csv上的文档: http://pandas.pydata.org/pandas-docs/stable/generation/pandas.read_csv.html

这篇关于pandas.read_csv:如何跳过注释行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆