Pythonic use of CSV module to skip headers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramon Felciano

    #1

    Pythonic use of CSV module to skip headers?

    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 (ignoreFirstLin e & 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
  • Steve Holden

    #2
    Re: Pythonic use of CSV module to skip headers?

    Ramon Felciano wrote:
    [color=blue]
    > 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 (ignoreFirstLin e & 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[/color]

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


    Holden Web LLC +1 800 494 3119

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: Pythonic use of CSV module to skip headers?

      In <76c29906.04120 21521.64ea904f@ posting.google. com>, Ramon Felciano
      wrote:
      [color=blue]
      > 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 (ignoreFirstLin e & line == 0):
      > continue
      > line = line+1
      > # do something with row[/color]

      What about:

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

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      Working...