csv - howto specify fmtparam parameters

H

Helmut Jarausch

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
 
P

Peter Otten

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

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

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

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

for s in dr:
print s

Peter
 
P

Peter Otten

Helmut said:
still I am curious: is the documentation in error?

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.__init__() 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
 
R

Raymond Hettinger

Peter Otten said:
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.__init__() 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)

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


Raymond Hettinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top