python 2.3, cvs module specific question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bernard Delmée

    python 2.3, cvs module specific question

    Hello, I can't seem to be able to specify the delimiter when building a
    DictReader()

    I can do:

    inf = file('data.csv' )
    rd = csv.reader( inf, delimiter=';' )
    for row in rd:
    # ...

    But this is rejected:

    inf = file('data.csv' )
    headers = inf.readline(). split(';')
    rd = csv.DictReader( inf, headers, delimiter=';' )
    for row in rd:
    # ...

    The DictReader constructor fails with a TypeError:
    init() got an unexpected keyword argument 'delimiter'
    Maybe I am missing something here?

    One rather convoluted workaround is the following:

    inf = file('data.csv' )
    d = csv.Sniffer().s niff(s)
    inf.seek(0)
    headers = inf.readline(). split(';')
    rd = csv.DictReader( inf, headers, dialect=d )
    for row in rd:
    # ...

    If DialectReader does indeed not accept the optional "fmtparam"
    then at least the documentation needs fixing. But then again
    I may just be misreading it....

    TIA,

    Bernard.

  • Peter Otten

    #2
    Re: python 2.3, cvs module specific question

    Bernard Delmée wrote:
    [color=blue]
    > Hello, I can't seem to be able to specify the delimiter when building a
    > DictReader()
    >[/color]

    DictReader currently does not accept parameters for creating a dialect on
    the fly. See Bug #792558.
    [color=blue]
    > One rather convoluted workaround is the following:
    >
    > inf = file('data.csv' )
    > d = csv.Sniffer().s niff(s)
    > inf.seek(0)
    > headers = inf.readline(). split(';')
    > rd = csv.DictReader( inf, headers, dialect=d )
    > for row in rd:
    > # ...
    >[/color]

    Another workaround might be (untested)

    class MyDialect(csv.e xcel):
    delimiter = ";"

    inf = file('data.csv' )
    headers = inf.readline(). split(';')
    rd = csv.DictReader( inf, headers, dialect=MyDiale ct)

    Of course your version should be more general, especially if you replace the
    literal ';':

    headers = inf.readline(). split(d.delimit er)


    Peter

    Comment

    • Skip Montanaro

      #3
      Re: python 2.3, cvs module specific question

      Bernard> But this is rejected:

      Bernard> inf = file('data.csv' )
      Bernard> headers = inf.readline(). split(';')
      Bernard> rd = csv.DictReader( inf, headers, delimiter=';' )
      Bernard> for row in rd:
      Bernard> # ...

      This is a known problem. For the time being, define a new dialect class and
      pass it in. If you know what the delimiter is, it's easier to define a
      class than call the sniffer.

      class semi(csv.excel) :
      delimiter = ';'

      inf = file('data.csv' )
      headers = inf.readline(). split(';')
      rd = csv.DictReader( f, headers, dialect=semi)
      for row in rd:
      ...

      Bernard> If DialectReader does indeed not accept the optional "fmtparam"
      Bernard> then at least the documentation needs fixing. But then again I
      Bernard> may just be misreading it....

      No, it's just a bug in my implementation.

      Skip

      Comment

      • Bernard Delmée

        #4
        Re: python 2.3, cvs module specific question

        > class semi(csv.excel) :[color=blue]
        > delimiter = ';'[/color]

        Thanks, guys, I hadn't thought of this simple solution.
        It solves my problem nicely.
        [color=blue]
        > No, it's just a bug in my implementation.[/color]

        This module as it is, is very useful, Skip. Frankly with
        the speed improvements, the csv, datetime and logging
        modules, python 2.3 is a really satisfying release.
        Rest assured that your effort does not go unnoticed.

        Cheers,

        Bernard.

        Comment

        Working...