Pythonic使用CSV模块跳过标题? [英] Pythonic use of CSV module to skip headers?

查看:50
本文介绍了Pythonic使用CSV模块跳过标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 -


我正在使用csv模块解析制表符分隔文件,并想知道

是否有更优雅的方式跳过可能的标题行。

我正在做


line = 0

reader = csv.reader(file (文件名))

读取行:

if(ignoreFirstLine& line == 0):

继续

line = line + 1

#用行做某事


我唯一能想到的就是专门设置默认阅读器

类带有额外的skipHeaderLine构造函数参数,这样它的

next()方法可以跳过适当的第一行。有没有其他的

更清洁的方式去做stdlib的子类化?


谢谢!


Ramon

解决方案

Ramon Felciano写道:

嗨 -

我''正在使用csv模块解析制表符分隔文件,并想知道是否有更优雅的方法来跳过可能的标题行。
我正在做
line = 0
reader = csv.reader(file(filename))
for reader中的行:
if(ignoreFirstLine& line == 0):
继续
line = line + 1
#用行做某事

我唯一能想到的是使用额外的skipHeaderLine构造函数参数来专门化默认的阅读器类,以便它的
next()方法可以跳过第一行合适的。是否还有其他更清洁的方法来完成stdlib的子类化?

谢谢!

Ramon



怎么样


line = 0

reader = csv.reader(file(filename))

headerline = reader.next()

读取行:

line = line + 1

#用行做某事
< br $>
问候

史蒂夫

-
http://www.holdenweb.com
http://pydish.holdenweb.com

Holden Web LLC +1 800 494 3119


In< 76 ** ************************@posting.google.com> ;, Ramon Felciano

写道:
< blockquote class =post_quotes>嗨 -

我正在使用csv模块解析制表符分隔的文件,并想知道是否有更优雅的方式来跳过可能的标题行。<我正在做读取行读取行:




读取器= csv.reader(文件(文件名))
如果(ignoreFirstLine) &安培; line == 0):
继续
line = line + 1
#用行做什么




怎么样


reader = csv.reader(file(filename))

reader.next()#Skip标题行。
读取行的


#用行做某事

Ciao,

Marc''BlackJack'' Rintsch


Ramon Felciano写道:

我正在使用csv模块解析制表符分隔文件并想知道<是否有一种更优雅的方式来跳过可能的标题行。
我正在做

line = 0
reader = csv.reader(文件(文件名) ))
读取行:
if(ignoreFirstLine& line == 0):
继续
line = line + 1
#对行做某事

我唯一能想到的是使用额外的skipHeaderLine构造函数参数来专门化默认的阅读器类,以便它的
next()方法可以跳过适当的第一行。是否还有其他更清洁的方法来完成继承stdlib的继承?


import csv
f = file(" tmp.csv")
f.next()
''#header \'''表示csv.reader(f)中的行:



....打印行

....

['' a'',''b'',''c'']

[''1'',''''''''''''

这样读者根本不需要弄乱标题。


彼得


Hi --

I''m using the csv module to parse a tab-delimited file and wondered
whether there was a more elegant way to skip an possible header line.
I''m doing

line = 0
reader = csv.reader(file(filename))
for row in reader:
if (ignoreFirstLine & line == 0):
continue
line = line+1
# do something with row

The only thing I could think of was to specialize the default reader
class with an extra skipHeaderLine constructor parameter so that its
next() method can skip the first line appropriate. Is there any other
cleaner way to do it w/out subclassing the stdlib?

Thanks!

Ramon

解决方案

Ramon Felciano wrote:

Hi --

I''m using the csv module to parse a tab-delimited file and wondered
whether there was a more elegant way to skip an possible header line.
I''m doing

line = 0
reader = csv.reader(file(filename))
for row in reader:
if (ignoreFirstLine & line == 0):
continue
line = line+1
# do something with row

The only thing I could think of was to specialize the default reader
class with an extra skipHeaderLine constructor parameter so that its
next() method can skip the first line appropriate. Is there any other
cleaner way to do it w/out subclassing the stdlib?

Thanks!

Ramon



How about

line = 0
reader = csv.reader(file(filename))
headerline = reader.next()
for row in reader:
line = line+1
# do something with row

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119


In <76**************************@posting.google.com >, Ramon Felciano
wrote:

Hi --

I''m using the csv module to parse a tab-delimited file and wondered
whether there was a more elegant way to skip an possible header line.
I''m doing

line = 0
reader = csv.reader(file(filename))
for row in reader:
if (ignoreFirstLine & line == 0):
continue
line = line+1
# do something with row



What about:

reader = csv.reader(file(filename))
reader.next() # Skip header line.
for row in reader:
# do something with row

Ciao,
Marc ''BlackJack'' Rintsch


Ramon Felciano wrote:

I''m using the csv module to parse a tab-delimited file and wondered
whether there was a more elegant way to skip an possible header line.
I''m doing

line = 0
reader = csv.reader(file(filename))
for row in reader:
if (ignoreFirstLine & line == 0):
continue
line = line+1
# do something with row

The only thing I could think of was to specialize the default reader
class with an extra skipHeaderLine constructor parameter so that its
next() method can skip the first line appropriate. Is there any other
cleaner way to do it w/out subclassing the stdlib?


import csv
f = file("tmp.csv")
f.next() ''# header\n'' for row in csv.reader(f):


.... print row
....
[''a'', ''b'', ''c'']
[''1'', ''2'', ''3'']

This way the reader need not mess with the header at all.

Peter


这篇关于Pythonic使用CSV模块跳过标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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