我在django模型中加載excel文件並使用xlrd讀取並在數據庫中存儲。 加載大約20k行花費1分鐘..但是我的文件有160k行花了8分鐘,所以我需要減少讀取和存儲的時間,以小於這種大容量文件的分鐘。我應該怎麼做才能減少加載時間。什麼是所有可能的方式?django批量excel文件讀取速度太慢使用xlrd
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Upload(file = request.FILES['file'])
newdoc.save()
xlbook = open_workbook(newdoc.file.name)
sheet = xlbook.sheet_by_index(0)
field = [sheet.cell_value(0,c) for c in range(sheet.ncols)]
temp = [sheet.cell_value(c,0) for c in range(sheet.nrows)]
value = [[sheet.cell_value(r,c) for c in range(sheet.ncols)]for r in range (sheet.nrows)]
data = {}
if request.POST.get('check'):
Sales.objects.filter(OrderID__in = temp[1:]).delete()
for n in range(0,sheet.nrows):
if n > 0:
list = value[n]
for i in range(0,len(field)):
data[field[i]] = list[i]
if data:
date_value = xldate_as_tuple(sheet.cell_value(n,1),xlbook.datemode)
data["Date"] = datetime(*date_value[:3])
add = Sales.objects.get_or_create(**data)
return HttpResponseRedirect('/salesdata/')
if request.method == 'GET':
form = UploadFileForm(request.POST, request.FILES)
return render(request,'salesdata/upload.html', {'form': form})
你的問題更多的是關於'xlrd'和更少關於'Django'的高效讀取。當然是 – Constantinius 2014-12-19 10:05:08
,是的!但有沒有什麼包可以更快地讀取excel併爲django模型編寫代碼。 – 2014-12-19 10:27:17