views:

204

answers:

2

i have some csv data and i want to export into django models the example of csv data

1;"02-01-101101";"Worm Gear HRF 50";"Ratio 1 : 10";"input shaft, output shaft, direction A, color dark green";
2;"02-01-101102";"Worm Gear HRF 50";"Ratio 1 : 20";"input shaft, output shaft, direction A, color dark green";
3;"02-01-101103";"Worm Gear HRF 50";"Ratio 1 : 30";"input shaft, output shaft, direction A, color dark green";
4;"02-01-101104";"Worm Gear HRF 50";"Ratio 1 : 40";"input shaft, output shaft, direction A, color dark green";
5;"02-01-101105";"Worm Gear HRF 50";"Ratio 1 : 50";"input shaft, output shaft, direction A, color dark green";

and i have some django models name Product in Product there is some fields like name, description and price

and i want to something like this

product=Product()

product.name = "Worm Gear HRF 70(02-01-101116)"

product.description = "input shaft, output shaft, direction A, color dark green"

product.price = 100

+2  A: 

The Python csv library can do your parsing and your code can translate them into Products().

msw
A: 

something like this:

f = open('data.txt', 'r')  
for line in f:  
   line =  line.split(';')  
   product = Product()  
   product.name = line[2] + '(' + line[1] + ')'  
   product.description = line[4]  
   product.price = '' #data is missing from file  
   product.save()  

f.close()  
DrDee