简体   繁体   English

在Django中导入csv

[英]Importing csv in Django

i am trying to import csv file into db from manage.py shell but it is giving me error 我正在尝试从manage.py shell将CSV文件导入db,但这给了我错误

import csv
with open(r"C:\Users\yousa\Desktop\xp\exp\Dist_Extended_TissuesWise_ClusteredAnnotated_21nov2018_qaz.csv", 'r') as f:
        reader = csv.reader(f)
        lines = list(reader)
        del lines[0]
        objects = []
        for line in lines:
            obj =Maize_clustert()
            obj.chromosome = int(line[0])
            obj.cluster_start = int(line[1])
            obj.cluster_end = int(line[2])
            obj.strand = line[3]
            obj.pac = int(line[4])
            obj.pac_suppoort = int(line[5])
            obj.cluster_support = int(line[6])
            obj.region = line[7]
            obj.gene_id = line[8]
            obj.transcript_id = line[9]
            obj.distance = line[10]
            obj.transcript_code = line[11]
            obj.gene_cord = line[12]
            obj.utr_length = int(line[13])
            obj.gene_biotype = line[14]
            obj.cluster_size = int(line[15])
            obj.number_pas = int(line[16])
            obj.zygote = int(line[17])
            obj.sperm = int(line[18])
            obj.egg = int(line[19])
            obj.root = int(line[20])
            obj.embryo = int(line[21])
            obj.basal = int(line[22])
            obj.ear = int(line[23])
            obj.apical = int(line[24])
            obj.ovule = int(line[24])
            objects.append(obj)
        Maize_clustert.objects.bulk_create(objects)

while runnig this code in manage.py shell it give me result 而在run.ig这个代码在manage.py shell中,它给我结果

Traceback (most recent call last):
  File "<input>", line 8, in <module>
NameError: name 'Maize_clustert' is not defined

while in models.py i have created full model of my data is there any alternative way or i am doing it wrong kindly help me in it 而在models.py中,我已经创建了我的数据的完整模型,是否有其他替代方法,或者我做错了,请帮助我。

Read info carefully: 仔细阅读信息:

NameError: name 'Maize_clustert' is not defined

That means that in file that you perform CSV reading this class is not defined. 这意味着在执行CSV读取的文件中未定义此类。 Thus even Maize_clustert exists in your models.py you have to import it in files that uses it. 因此,即使您的models.py中也存在Maize_clustert,您也必须将其导入使用它的文件中。

Basically add this at top of your file: 基本上将其添加到文件顶部:

`from application.models import Maize_clustert 从application.models导入Maize_clustert

You need to import Maize_clustert in the file. 您需要在文件中导入Maize_clustert Like: 喜欢:

# Please read the PEP-8 Style Guide on Naming convention
# Class Name should be 'PascalCase'
from yourapp.models import Maize_clustert

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM