pygrametl CSVSource TypeError:init()至少接受2个参数(给定1个) [英] pygrametl CSVSource TypeError: init() takes at least 2 arguments (1 given)
问题描述
我正在尝试使用pygrametl CSVSource,如文档
I am trying to use pygrametl CSVSource as shown in the documentation
这是我的代码
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 ()至少接受2个参数(给定1个参数)
TypeError: init() takes at least 2 arguments (1 given)
我该如何解决?
推荐答案
从您提到的文档中,我们可以看到CSVSource
只是对csv
模块中的DictReader
的引用.
From the documentation You mentioned, we can see that CSVSource
is just reference to DictReader
from csv
module.
如果我们查看DictReader
类的源代码(准确地说是__init__
方法),就会看到以下内容:
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
由于输入自变量中没有关键字csvfile
,因此此自变量将传递给**kwds
,这意味着缺少自变量f
.我没有安装此库,但是我认为仅通过open('src.csv', 'r', 16384)
而没有csvfile=
可以解决此问题.像这样:
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=',')
更新:只需安装pygrametl
并在没有csvfile=
的情况下进行测试,就可以正常工作.
Update: Just installed pygrametl
and tested without csvfile=
, it works fine.
这篇关于pygrametl CSVSource TypeError:init()至少接受2个参数(给定1个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!