django import csv文件

时间:2014-08-05 09:43:55

标签: django csv import

我有我的模特

class Essai_Temperature(models.Model):
   name = models.ForeignKey(Material, verbose_name=_('name'))                                    # 
   nature_unit = models.ForeignKey(Property, verbose_name=_('category'))                      # 
   choix = ChainedForeignKey(Physic, verbose_name=_('properties'), null=True, blank=True,
                          related_name='Essai_Temperature_choix',
                          chained_field="nature_unit",
                          chained_model_field="name",
                          show_all=False,
                          auto_choose=True) 
   valT= models.FloatField(_('temperature'),blank=True, null=False)   
   val10= models.FloatField(_('value'), blank=True, null=False)                                               # valeur numerique min
   val_ref= models.CharField(_('reference of the data'), max_length=50, default='0')    

我想读取一个csv文件并将数据写入数据库

我已写入管理文件

def import_csv(modeladmin, request, queryset):
    import csv, sys
    filename = 'C:/Users/patrice/Documents/base/data50.csv'
    dataReader = csv.reader(open(filename), delimiter=';', quotechar='"')

    for row in dataReader:
        try:

        essai_temperature = Essai_Temperature()

        essai_temperature.name__name = row[1]
        essai_temperature.nature_unit__name = row[2]
        essai_temperature.choix__lapropriete = row[3]
        essai_temperature.valT = float(row[4]) if row[4] is not None else None 
        essai_temperature.val10 = float(row[5]) if row[5] is not None else None
        essai_temperature.val_ref = row[6]
        essai_temperature.save()

    except csv.Error as e:
        sys.exit('file %s, line %d: %s' % (filename, dataReader.line_num, e))

dataReader.close()

它不起作用,我有错误:

例外值:

无法将字符串转换为float:valT

我不明白为什么?

0 个答案:

没有答案