我有這個測試內容的CSV文件:Python的CSV模塊中表現不同的Django視圖
1,2,3
4,5,6
7,8,9
與csv模塊通常的方式解析這工作得很好:
>>> for row in csv.reader(open('test.csv')):
... print row
...
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']
然後我嘗試,並在Django視圖使用相同的過程:
def upload_csv(request):
if request.method == 'POST':
form = ProductCSVUploadForm(request.POST, request.FILES)
if form.is_valid():
for row in csv.reader(request.FILES['csv_file'].read()):
print row
else:
form = ProductCSVUploadForm()
return render_to_response('upload_csv.html', locals())
這給出的輸出:
['1']
['', '']
['2']
['', '']
['3']
[]
['4']
['', '']
['5']
['', '']
['6']
[]
['7']
['', '']
['8']
['', '']
['9']
[]
是Django更改數據?我怎樣才能阻止這種情況發生?
呵呵,發生在我身上有些時候:我寫了一個問題,當我校對它,答案來找我。 :) 祝你好運。 –