Search code examples
pythoncsvtypeerrorpygrametl

pygrametl CSVSource TypeError: init() takes at least 2 arguments (1 given)


I am trying to use pygrametl CSVSource as shown in the documentation

This is my code

import pygrametl
from pygrametl.datasources import CSVSource

src = CSVSource(csvfile=open('src.csv', 'r', 16384), \
                            delimiter=',')

but I get the following error even though I use the exact code.

TypeError: init() takes at least 2 arguments (1 given)

How can I fix this?


Solution

  • From the documentation You mentioned, we can see that CSVSource is just reference to DictReader from csv module.

    If we look at the source code of DictReader class (it's __init__ method, to be precise), we see this:

    class DictReader:
         def __init__(self, f, fieldnames=None, restkey=None, restval=None,
                      dialect="excel", *args, **kwds):
             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, **kwds)
             self.dialect = dialect
             self.line_num = 0
    

    Since there is no keyword csvfile in the input arguments, this argument is passed to **kwds, meaning argument f is missing. I don't have this library installed, but I think that just passing open('src.csv', 'r', 16384) without csvfile= will fix this issue. Something like this:

    import pygrametl
    from pygrametl.datasources import CSVSource
    
    src = CSVSource(open('src.csv', 'r', 16384), delimiter=',')
    

    Update: Just installed pygrametl and tested without csvfile=, it works fine.