Python 从csv填充模型:初始化参数错误
我有一个包含大量列的表,需要从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
\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)