I have a Django view which reads a CSV file and saves it to a model.
views.py:
def csv_upload(request):
...
file = request.FILES['attach']
reader = csv.reader(file, delimiter=';')
next(reader) # skip headers
for line in reader:
... # process and save
EDIT
Traceback:
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/sam/django-projects/datazone/bin/datazone/climate/views.py" in climate_upload
258. report.year = line[1]
Exception Type: IndexError at /climate/upload/
Exception Value: list index out of range
END EDIT
Whilst testing with file variations, I noticed that if there is trailing whitespace in the file (e.g. empty rows due to saving form Excel) I get an Index out of range error.
My question is, how can I strip the whitespace from the end (and probably the begining just to be sure) of the file.
Any help much appreciated.
if my understanding is correct, you want to take care of some blank lines in the end of the file, right?
Thanks @dm03514 for pointing out a mistake in my original answer