csv - howto specify fmtparam parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Helmut Jarausch

    csv - howto specify fmtparam parameters

    Hi,

    according to the docs the following should work (IMHO)

    import csv
    csv_file= file('test.csv' )

    Inp= csv.DictReader( csv_file,['Matr','Name',' Vorname','PZ',' MP'],\
    lineterminator= '\n')

    for S in Inp:
    print S

    but I get
    Traceback (most recent call last):
    File "/home/jarausch/Python/My/test_csv.py", line 5, in -toplevel-
    lineterminator= '\n')
    TypeError: __init__() got an unexpected keyword argument 'lineterminator '

    What went wrong?

    Thanks for a hint,


    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany

  • Peter Otten

    #2
    Re: csv - howto specify fmtparam parameters

    Seems that the docs are wrong. The following should work:

    import csv
    csv_file = file('test.csv' )

    class MyDialect(csv.e xcel):
    lineterminator = "\n"

    dr = csv.DictReader( csv_file,['Matr','Name',' Vorname','PZ',' MP'],\
    dialect=MyDiale ct)

    for s in dr:
    print s

    Peter

    Comment

    • Peter Otten

      #3
      Re: csv documentation error? (was Re: csv - howto specify fmtparam parameters)

      Helmut Jarausch wrote:
      [color=blue]
      > still I am curious: is the documentation in error?[/color]

      Taken directly from csv.py:

      class DictReader:
      def __init__(self, f, fieldnames, restkey=None, restval=None,
      dialect="excel" , *args):
      self.fieldnames = fieldnames # list of keys for the dict
      self.restkey = restkey # key to catch long rows
      self.restval = restval # default value for short rows
      self.reader = reader(f, dialect, *args)

      As there is no provision for keyword arguments in DictReader.__in it__() as
      opposed to reader, it seems to be a bug that can be easily fixed:

      class DictReader:
      def __init__(self, f, fieldnames, restkey=None, restval=None,
      dialect="excel" , *args, **kwd):
      self.fieldnames = fieldnames # list of keys for the dict
      self.restkey = restkey # key to catch long rows
      self.restval = restval # default value for short rows
      self.reader = reader(f, dialect, *args, **kwd)

      Peter

      Comment

      • Raymond Hettinger

        #4
        Re: csv documentation error? (was Re: csv - howto specify fmtparam parameters)


        "Peter Otten" <__peter__@web. de> wrote in message
        news:bi26pd$dug [email protected]...[color=blue]
        > Helmut Jarausch wrote:
        >[color=green]
        > > still I am curious: is the documentation in error?[/color]
        >
        > Taken directly from csv.py:
        >
        > class DictReader:
        > def __init__(self, f, fieldnames, restkey=None, restval=None,
        > dialect="excel" , *args):
        > self.fieldnames = fieldnames # list of keys for the dict
        > self.restkey = restkey # key to catch long rows
        > self.restval = restval # default value for short rows
        > self.reader = reader(f, dialect, *args)
        >
        > As there is no provision for keyword arguments in DictReader.__in it__() as
        > opposed to reader, it seems to be a bug that can be easily fixed:
        >
        > class DictReader:
        > def __init__(self, f, fieldnames, restkey=None, restval=None,
        > dialect="excel" , *args, **kwd):
        > self.fieldnames = fieldnames # list of keys for the dict
        > self.restkey = restkey # key to catch long rows
        > self.restval = restval # default value for short rows
        > self.reader = reader(f, dialect, *args, **kwd)[/color]

        Please file a bug report so this doesn't get lost.


        Raymond Hettinger


        Comment

        Working...