如何将CSV数据导入Django模型
我有一些CSV格式的数据,想把它们导入到Django模型中,下面是一个示例的CSV数据:
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";
我有一个名为Product的Django模型。在这个Product模型里,有一些字段,比如name
(名字)、description
(描述)和price
(价格)。我想要的效果大概是这样的:
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
16 个回答
使用Pandas库来创建一个包含csv数据的数据框(dataframe)。
你可以通过在csv文件的第一行包含字段名,或者在代码中使用数据框的columns方法来命名这些字段。
接着,创建一个模型实例的列表。
最后,使用django的方法.bulk_create()将你的模型实例列表发送到数据库表中。
Pandas中的read_csv函数非常适合读取csv文件,并且提供了很多参数,可以跳过某些行、忽略某些字段等等。
import pandas as pd
from app.models import Product
tmp_data=pd.read_csv('file.csv',sep=';')
#ensure fields are named~ID,Product_ID,Name,Ratio,Description
#concatenate name and Product_id to make a new field a la Dr.Dee's answer
products = [
Product(
name = tmp_data.ix[row]['Name'],
description = tmp_data.ix[row]['Description'],
price = tmp_data.ix[row]['price'],
)
for row in tmp_data['ID']
]
Product.objects.bulk_create(products)
我之前使用mmrs151的回答,但保存每一行(实例)非常慢,而且任何包含分隔符的字段(即使在引号内)也没有被open() -- line.split(';')方法处理。
Pandas有很多有用的注意事项,值得花时间去了解。
如果你想使用一个库,快速在网上搜索 csv
和 django
可以找到两个库 - django-csvimport 和 django-adaptors。我们来看看它们各自的介绍...
- django-adaptors:
Django adaptor 是一个工具,可以让你轻松地把 CSV/XML 文件转换成 Python 对象或者 Django 模型实例。
- django-importcsv:
django-csvimport 是一个通用的导入工具,可以让你上传 CSV 文件来填充数据。
第一个库需要你写一个模型来匹配 CSV 文件,而第二个库更像是一个命令行导入工具,这在使用方式上有很大的不同,每个库适合不同类型的项目。
那么该用哪个呢?这要看哪个库更适合你的项目,长期来看哪个更好。
不过,你也可以完全不使用库,自己写一个 Django 脚本 来导入你的 CSV 文件,类似于(注意,这里是伪代码):
# open file & create csvreader
import csv, yada yada yada
# import the relevant model
from myproject.models import Foo
#loop:
for line in csv file:
line = parse line to a list
# add some custom validation\parsing for some of the fields
foo = Foo(fieldname1=line[1], fieldname2=line[2] ... etc. )
try:
foo.save()
except:
# if the're a problem anywhere, you wanna know about it
print "there was a problem with line", i
这非常简单。其实,如果只是一次性导入,你甚至可以通过 Django shell 交互式地完成。只要弄清楚你想在项目中做什么,需要处理多少个文件,然后如果决定使用库,就试着找出哪个更符合 你的需求。
你想使用Python语言中的csv模块,并且应该使用Django的get_or_create方法。
with open(path) as f:
reader = csv.reader(f)
for row in reader:
_, created = Teacher.objects.get_or_create(
first_name=row[0],
last_name=row[1],
middle_name=row[2],
)
# creates a tuple of the new object or
# current object and a boolean of if it was created
在我的例子中,教师模型有三个属性:名字(first_name)、姓氏(last_name)和中间名(middle_name)。
关于get_or_create方法的详细说明可以查看Django的文档。