By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

csv - howto specify fmtparam parameters

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
Helmut Jarausch wrote:
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
Jul 18 '05 #3

P: n/a

"Peter Otten" <__*******@web.de> wrote in message
news:bi*************@news.t-online.com...
Helmut Jarausch wrote:
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)


Please file a bug report so this doesn't get lost.
Raymond Hettinger
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.