Sie sind auf Seite 1von 3

6/7/2016

python - how to import csv data into django models - Stack Overow

help

howtoimportcsvdataintodjangomodels
IhavesomeCSVdataandIwanttoimportintodjangomodelsusingtheexampleCSVdata:
1"0201101101""WormGearHRF50""Ratio1:10""inputshaft,outputshaft,
directionA,colordarkgreen"
2"0201101102""WormGearHRF50""Ratio1:20""inputshaft,outputshaft,
directionA,colordarkgreen"
3"0201101103""WormGearHRF50""Ratio1:30""inputshaft,outputshaft,
directionA,colordarkgreen"
4"0201101104""WormGearHRF50""Ratio1:40""inputshaft,outputshaft,
directionA,colordarkgreen"
5"0201101105""WormGearHRF50""Ratio1:50""inputshaft,outputshaft,
directionA,colordarkgreen"

IhavesomedjangomodelsnamedProduct.InProducttherearesomefieldslike
somethinglikethis:

name, descriptionand price.Iwant

product=Product()
product.name="WormGearHRF70(0201101116)"
product.description="inputshaft,outputshaft,directionA,colordarkgreen"
product.price=100
python django csv djangomodels export
editedDec1'14at9:44

norbertpy
959

askedMar17'10at4:59

little_fish
9

36

439

1 <docs.pythontablib.org/en/latest/index.html>isalsoanamazinglibraryforthis.CraigLabenzJun22'12
at15:05

11Answers

Youwanttousethecsvmodulethatispartofthepythonlanguageandyoushoulduse
Django'sget_or_createmethod
withopen(path)asf:
reader=csv.reader(f)
forrowinreader:
_,created=Teacher.objects.get_or_create(
first_name=row[0],
last_name=row[1],
middle_name=row[2],
)
#createsatupleofthenewobjector
#currentobjectandabooleanofifitwascreated

Inmyexamplethemodelteacherhasthreeattributesfirst_name,last_nameand
middle_name.
Djangodocumentationofget_or_createmethod
editedJan27at16:57

answeredApr12'13at1:41

AlexPanov

Friendm1

125

470

Itoldyouinacommentwhyyoushouldn'tbeaskingthis.Butyou'renewandIlikenewpeople
whocometoSO.Firstofall,lookthere'sawaytoaskforrecommendationsitgenerally
revolvesaroundbeingspecificaboutwhatyou'retryingtoachieve.Becauseusually,
differentlibrarieshavedifferentstrongpoints.
You'realsoexpectedtodosomeminimalefforttoresearchyourproblemyourself.Inthiscase,
aquickgooglesearchfor csvand djangorevealsthetwolibrariesthatyoumentioned
immediatlydjangocsvimportanddjangoadaptors.Sowhatshouldyouuse??Hmm...Let's
readwhattheyhavetosayaboutthemselves...
djangoadaptors:
DjangoadaptorisatoolwhichallowyoutotransformeasilyaCSV/XMLfileintoapython

http://stackoverow.com/questions/2459979/how-to-import-csv-data-into-django-models

1/5

6/7/2016

python - how to import csv data into django models - Stack Overow

objectoradjangomodelinstance.
djangoimportcsv:
djangocsvimportisagenericimportertooltoallowtheuploadofCSVfilesforpopulating
data.
Sorightofthebatyoucanclearlyseethatthesetwolibrariesyoumentiondocompletely
differentthingsthefirstrequiresyoutowriteamodeltomatchthecsvfile,whilethesecond
ismoreofacommandlineimporter.That'sahugedifferenceinthewayyouworkwiththem,
andeachisgoodforadifferenttypeofproject.
Sowhatyoushouldbeaskingisthiswhatbetterfitstheneedofyourproject?Whatisthe
endgoal?Whatlibrarywillbettersuityouforthelongrun?
Andalsowhyusealibraryatall?What'ssobadaboutwritingyourowndjangoscriptto
importyourcsvfile?It'sreallysimple.It'susuallysomethinglikethis(warning,pseudocode
ahead):
#openfile&createcsvreader
importcsv,yadayadayada
#importtherelevantmodel
frommyproject.modelsimportFoo
#loop:
forlineincsvfile:
line=parselinetoalist
#addsomecustomvalidation\parsingforsomeofthefields
foo=Foo(fieldname1=line[1],fieldname2=line[2]...etc.)
try:
foo.save()
except:
#ifthe'reaproblemanywhere,youwannaknowaboutit
print"therewasaproblemwithline",i

I'vedoneitamilliontimesbefore.It'ssupereasy.Hell,youcandoitinteractivelythroughthe
djangoshell.Justfigureoutwhatyouwanttodowithyourproject,howmanyfilesdoyou
needtohandleandthenifyoudecidetousealibrary,tryfiguringoutwhichonebettersuits
yourneeds.
Whenyou'vethroughallthat,andyoustillgotdoubts,that'swhenyoucometoSOandask
"heyguys,I'vebeencheckingthesetwolibrariesandIcan'tdecide.Thisonelooksgood
becausethisandthat,buttheotheronehasthisfeaturethatIneed".
That'stherightwaytoaskbecausethatwayyou'rebothbeingspecificandprovingyou've
doneyourhomework.Thereareprofessionalpeopleherewillingtospendtheirtimehelping
you,theminimumtheyexpecttoseeisyouputsomeeffortintothequestion.Alright?
answeredJul10'14at6:12

yuvi
9,221

26

52

FYI:mergedfromstackoverflow.com/questions/24660654/csvfileimporttodjangoShog9 Jul24'14at

19:11
@Shog9Ionlynownoticedthis.Idon'tgetit,why?thosetwoquestionsaren'trelatedatall,andthismakes

myentireanswerintoanonsensicalexplanationforsomethingcompletelydifferentthanwhattheOPasks
for.Shouldn'titbemergedtheotherwayaroundmaybe?yuviNov3'14at13:22
Bothquestionsaddressthesameproblem,butthisisabetterquestionintermsofprovidinganexampleto

others.Ifyoudon'tmindediting,ithinkyouranswercanbeusefulhere.Shog9 Nov3'14at17:13
@Shog9ok,Iseeyourpoint.Idon'tmindediting,I'llgettoitwhenIcan.ThanksforthereplyyuviNov3

'14at18:11
3 "aquickgooglesearchforcsvanddjango"...gotmetothispage:pChrisHuangLeaverFeb2at1:11

ThePythoncsvlibrarycandoyourparsingandyourcodecantranslatetheminto
Products().
answeredMar17'10at5:04

msw
31.2k

45

87

somethinglikethis:

http://stackoverow.com/questions/2459979/how-to-import-csv-data-into-django-models

2/5

6/7/2016

python - how to import csv data into django models - Stack Overow

f=open('data.txt','r')
forlineinf:
line=line.split('')
product=Product()
product.name=line[2]+'('+line[1]+')'
product.description=line[4]
product.price=''#dataismissingfromfile
product.save()
f.close()
answeredMar17'10at13:36

DrDee
2,076

20

34

Youcanalsouse,djangoadaptors
>>>fromadaptor.modelimportCsvModel
>>>classMyCSvModel(CsvModel):
...name=CharField()
...age=IntegerField()
...length=FloatField()
...
...classMeta:
...delimiter=""

YoudeclareaMyCsvModelwhichwillmatchtoaCSVfilelikethis:
Anthony271.75
Toimportthefileoranyiterableobject,justdo:
>>>my_csv_list=MyCsvModel.import_data(data=open("my_csv_file_name.csv"))
>>>first_line=my_csv_list[0]
>>>first_line.age
27

Withoutanexplicitdeclaration,dataandcolumnsarematchedinthesameorder:
Anthony>Column0>Field0>name
27>Column1>Field1>age
1.75>Column2>Field2>length
answeredOct5'12at23:16

mmrs151
1,748

19

25

DoyouknowthemethodtocorrectwritetheupdateargumentinMetaclassinsidemyCsvmodel?I'mtrying

todoitbutIgotaKeyError.CristianRojasNov28'12at21:32

Youcanusethedjangocsvimporterpackage.http://pypi.python.org/pypi/djangocsv
importer/0.1.1
Itworkslikeadjangomodel
MyCsvModel(CsvModel):
field1=IntegerField()
field2=CharField()
etc
classMeta:
delimiter=""
dbModel=Product

Andyoujusthaveto:CsvModel.import_from_file("myfile")
Thatwillautomaticallycreateyourproducts.
answeredOct2'11at9:27

trez
378

3 djangocsvimporterisnomoremaintainedinfavorofdjangoadaptorstrezDec29'12at8:59

Here'sadjangoeggforit:

http://stackoverow.com/questions/2459979/how-to-import-csv-data-into-django-models

3/5

Das könnte Ihnen auch gefallen