簡體   English   中英

pygrametl CSVSource TypeError:init()至少接受2個參數(給定1個)

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

我正在嘗試使用pygrametl CSVSource,如文檔所示

這是我的代碼

import pygrametl
from pygrametl.datasources import CSVSource

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

但是即使使用了正確的代碼,我仍然收到以下錯誤。

TypeError: init ()至少接受2個參數(給定1個)

我怎樣才能解決這個問題?

從您提到的文檔中,我們可以看到CSVSource只是csv模塊中對DictReader引用。

如果我們看一下DictReader類的源代碼( DictReader__init__方法),就會看到:

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=可以解決此問題。 像這樣:

import pygrametl
from pygrametl.datasources import CSVSource

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

更新 :剛剛安裝pygrametl並在沒有csvfile=情況下進行了測試,它可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM