I have a csv file with this test content:
1,2,3
4,5,6
7,8,9
Parsing this in the usual manner with the csv module works fine:
>>> for row in csv.reader(open('test.csv')):
... print row
...
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']
I then try and use the same process in a django view:
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())
This gives the output:
['1']
['', '']
['2']
['', '']
['3']
[]
['4']
['', '']
['5']
['', '']
['6']
[]
['7']
['', '']
['8']
['', '']
['9']
[]
Is Django changing the data? How can I stop this from happening?
Matt added to the top of the question:
The reason for the problem is
readreturns a string, which gives characters when iterated over, whileopenproduces a file object, which produces lines when iterated over.