带评论的CSV [英] CSV with comments

查看:63
本文介绍了带评论的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在csv.reader中,是否有任何跳过行的方式可以启动whith''#''或

空行

我会在我的CSV文件中添加注释

解决方案



GinTon写道:


在csv.reader中,是否有任何跳过行的方式可以启动whith''#''或

空行

我会在我的CSV文件中添加注释



对于跳过评论我得到一个肮脏的把戏:


reader = csv.reader(open(csv_file))
读取中csv_line的


如果csv_line [0] .startswith(''#''):

继续


但是不可能让空行。


我认为CSV类应该允许跳过评论和

auto的新行。方式。


GinTon写道:


GinTon写道:


>在csv.reader中,是否有任何跳过行的方式来启动#或
空行
我会在我的CSV文件中添加注释



对于跳过评论我得到一个肮脏的把戏:


reader = csv.reader(open(csv_file))

为读者中的csv_line:

如果csv_line [0] .startswith(''#''):

继续

但是不可能让空行。


我认为CSV类应该允许跳过评论和

auto的新行。方式。



编写一个迭代器,根据自己的喜好过滤行并将其用作输入

到cvs.reader:


def CommentStripper(iterator):

for the iterator:

if line [:1] ==''#'':

继续

如果不是line.strip():

继续

收益率线


reader = csv.reader(CommentStripper(open(csv_file)))

CommentStripper实际上对其他文件非常有用。当然

如果评论开始可能会有差异

- 在第一个字符上

- 在第一个非空白字符上
- 行中的任何地方


Daniel


>在csv.reader中,有没有办法跳过开始whith''#''或


>空行



不。当我们编写模块时,我们并不知道任何规范。那个

指定的评论或空白行。您可以轻松编写一个文件包装器来

过滤掉它们:


class BlankCommentCSVFile:

def __init __(self,fp ):

self.fp = fp


def __iter __(个体经营):

返回自我

def next(self):

line = self.fp.next()

如果不是line.strip()或line [0] == "#":

返回self.next()

返回行


使用它如下:


reader = csv.reader(BlankCommentCSVFile(open(" somefile.csv"))))

为读者行:

打印行


跳过


In csv.reader, is there any way of skip lines that start whith ''#'' or
empty lines
I would add comments at my CSV file

解决方案


GinTon wrote:

In csv.reader, is there any way of skip lines that start whith ''#'' or
empty lines
I would add comments at my CSV file

For skip comment I get a dirty trick:

reader = csv.reader(open(csv_file))
for csv_line in reader:
if csv_line[0].startswith(''#''):
continue

But not possible let blank lines.

I think that CSV class should to let skip comments and new lines of
auto. way.


GinTon wrote:

GinTon wrote:

>In csv.reader, is there any way of skip lines that start whith ''#'' or
empty lines
I would add comments at my CSV file


For skip comment I get a dirty trick:

reader = csv.reader(open(csv_file))
for csv_line in reader:
if csv_line[0].startswith(''#''):
continue

But not possible let blank lines.

I think that CSV class should to let skip comments and new lines of
auto. way.

write an iterator that filters line to your liking and use it as input
to cvs.reader:

def CommentStripper (iterator):
for line in iterator:
if line [:1] == ''#'':
continue
if not line.strip ():
continue
yield line

reader = csv.reader (CommentStripper (open (csv_file)))

CommentStripper is actually quite useful for other files. Of course
there might be differences if a comment starts
- on the first character
- on the first non-blank character
- anywhere in the line

Daniel


>In csv.reader, is there any way of skip lines that start whith ''#'' or

>empty lines

Nope. When we wrote the module we weren''t aware of any "spec" that
specified comments or blank lines. You can easily write a file wrapper to
filter them out though:

class BlankCommentCSVFile:
def __init__(self, fp):
self.fp = fp

def __iter__(self):
return self

def next(self):
line = self.fp.next()
if not line.strip() or line[0] == "#":
return self.next()
return line

Use it like so:

reader = csv.reader(BlankCommentCSVFile(open("somefile.csv" )))
for row in reader:
print row

Skip


这篇关于带评论的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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