Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从csv填充模型:初始化参数错误_Python_Database_Sqlalchemy_Flask - Fatal编程技术网

Python 从csv填充模型:初始化参数错误

Python 从csv填充模型:初始化参数错误,python,database,sqlalchemy,flask,Python,Database,Sqlalchemy,Flask,我有一个包含大量列的表,需要从csv文件填充这些列。我在模型定义中有以下\uuuuu init\uuuu代码 从csv文件中读取的代码是(load_csv.py) 执行load_csv.py时出现以下错误 data_file = "data.csv" csv_file = csv.DictReader(open(data_file, 'rU'), delimiter=',') for row in csv_file: table_entries = {} for

我有一个包含大量列的表,需要从csv文件填充这些列。我在模型定义中有以下
\uuuuu init\uuuu
代码

从csv文件中读取的代码是(load_csv.py)

执行load_csv.py时出现以下错误

data_file = "data.csv"
csv_file = csv.DictReader(open(data_file, 'rU'), delimiter=',')
for row in csv_file:
        table_entries = {}
        for key, value in row.items():
                table_entries[key] = value
        table_row = Table(table_entries)
        db.session.add(table_row)
        db.session.commit()
    table_row = Table(table_entries)
TypeError: __init__() takes exactly 1 argument (2 given)

我读到这篇文章是因为它使用了默认的
\uuuuu init\uuuu
,但我不明白为什么它缺少我在代码中定义的
\uuuuuuu init\uuu
函数。如果您能帮助解决这个问题,我们将不胜感激

要将字典作为关键字参数应用:

table_row = Table(**table_entries)
或者更改
Table()
类以接收一个参数:

class Table

    def __init__(self, row):
            self.__dict__.update(row)
class Table

    def __init__(self, row):
            self.__dict__.update(row)