我使用django,我試圖將CSV_data列表導出到csv文件中。下面是我的csv.py:django輸出空csv
#coding=utf-8
from django.http import HttpResponse
from django.template import loader, Context
from demo.views import CSV_data
def output(request, filename):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
t = loader.get_template('csv.txt')
c = Context({
'data': CSV_data,
})
response.write(t.render(c))
return response
CSV_data是views.py一個變量,我試圖打印模板,該值是確定的。
[(u'2012-06-01', [0, 0, 0]), ('2012-06-08', [0, 0, 0]), ('2012-06-15', [0, 0, 0]), ('2012-06-22', [0, 0, 0]), ('2012-06-29', [0, 0, 0]), ('2012-07-06', [0, 0, 0]), ('2012-07-13', [0, 0, 0]), ('2012-07-20', [0, 0, 0]), ('2012-07-27', [0, 0, 0]), ('2012-08-03', [131, 164, 79.88]), ('2012-08-10', [110, 198, 55.56]), ('2012-08-17', [112, 197, 56.85]), ('2012-08-24', [147, 283, 51.94]), ('2012-08-31', [0, 306, 0.0]), ('2012-09-07', [418, 418, 100.0]), ('2012-09-14', [342, 342, 100.0]), ('2012-09-21', [732, 732, 100.0]), ('2012-09-28', [689, 689, 100.0]), ('2012-10-05', [775, 775, 100.0]), ('2012-10-12', [469, 469, 100.0]), ('2012-10-19', [477, 477, 100.0]), ('2012-10-26', [897, 897, 100.0]), ('2012-11-02', [216, 216, 100.0]), ('2012-11-09', [1046, 1046, 100.0]), ('2012-11-16', [840, 840, 100.0]), ('2012-11-23', [948, 948, 100.0])]
但是,生成的csv始終爲空。
我試圖CSV_data定義添加到csv.py文件,像這樣:
#coding=utf-8
from django.http import HttpResponse
from django.template import loader, Context
CSV_data = [(u'2012-06-01', [0, 0, 0]), ('2012-06-08', [0, 0, 0]), ('2012-06-15', [0, 0, 0]), ('2012-06-22', [0, 0, 0]), ('2012-06-29', [0, 0, 0]), ('2012-07-06', [0, 0, 0]), ('2012-07-13', [0, 0, 0]), ('2012-07-20', [0, 0, 0]), ('2012-07-27', [0, 0, 0]), ('2012-08-03', [131, 164, 79.88]), ('2012-08-10', [110, 198, 55.56]), ('2012-08-17', [112, 197, 56.85]), ('2012-08-24', [147, 283, 51.94]), ('2012-08-31', [0, 306, 0.0]), ('2012-09-07', [418, 418, 100.0]), ('2012-09-14', [342, 342, 100.0]), ('2012-09-21', [732, 732, 100.0]), ('2012-09-28', [689, 689, 100.0]), ('2012-10-05', [775, 775, 100.0]), ('2012-10-12', [469, 469, 100.0]), ('2012-10-19', [477, 477, 100.0]), ('2012-10-26', [897, 897, 100.0]), ('2012-11-02', [216, 216, 100.0]), ('2012-11-09', [1046, 1046, 100.0]), ('2012-11-16', [840, 840, 100.0]), ('2012-11-23', [948, 948, 100.0])]
def output(request, filename):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
t = loader.get_template('csv.txt')
c = Context({
'data': CSV_data,
})
response.write(t.render(c))
return response
然後輸出CSV是不是空的。所以我猜想從views.py導入CSV_data時會出現問題。
問題是我測試過,視圖中的CSV_data值是正確的。那麼會出現什麼問題呢?
** * ** * ** * ** * ** * *UPDATE* ** * ** * ** * ** * ** *:
在views.py原來的代碼是這樣的:
CSV_data = []
def part_usage_result(request):
...(details omit)
usageDictWeek = helper.getResultByWeek(modelName, spareCode, start, end) #returns a list
CSV_data=usageDictWeek
我更改爲:
CSV_data = []
def part_usage_result(request):
...(details omit)
usageDictWeek = helper.getResultByWeek(modelName, spareCode, start, end) #returns a list
for each in usageDictWeek:
CSV_data.append(each)
現在CSV的內容是正確的。 仍然不知道爲什麼會這樣
你必須更深入:)「helper.getResultByWeek」的相關代碼是什麼?如何調用'part_usage_result'? CSV_data [:] = usageDictWeek有幫助嗎? – alko
是CSV_data [:] = usageDictWeek的作品!我想我需要學習更多的Python :) – alexZ