从 csv 导入数据时,我意识到如果第一行不是标题,则会触发此错误
list indices must be integers or slices, not str
first_name,last_name,email,password,role
Noak,Larrett,nlarrett0@ezinearticles.com,8sh15apPjI,Student
Duffie,Milesap,dmilesap1@wikipedia.org,bKNIlIWVfNw,Student
仅当第一行是标题时才有效
first_name,last_name,email,password,role
Noak,Larrett,nlarrett0@ezinearticles.com,8sh15apPjI,Student
Duffie,Milesap,dmilesap1@wikipedia.org,bKNIlIWVfNw,Student
...
我尝试覆盖before_import以删除任何空白行
def before_import(self, dataset, using_transactions, dry_run, **kwargs):
indexes = []
for i in range(0, len(dataset)):
row = ''.join(dataset[i])
if row.strip() == '':
indexes.append(i)
for index in sorted(indexes, reverse=True):
del dataset[index]
return dataset
这适用于所有行,除了应始终包含标题的第一行,否则将引发错误。
达令说
相关分类