Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8141667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:35:17+00:00 2026-06-06T12:35:17+00:00

I have a Django view which reads a CSV file and saves it to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:35:19+00:00Added an answer on June 6, 2026 at 12:35 pm

    if my understanding is correct, you want to take care of some blank lines in the end of the file, right?

    for line in reader:
        if line:
            ... # process and save
    

    Thanks @dm03514 for pointing out a mistake in my original answer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view in a Django 1.4 project: def index(request): print reverse('menus_index') latest_menu_list
In my django app, I have a view which accomplishes file upload.The core snippet
Hi I am writing a Django view which generates a csv file. Pretty straightforward.
I have a Django project. Given a url, how can I know which view
I have a django view that returns HTTP 301 on a curl request: grapefruit:~
I have a Django view called change_priority. I'm posting a request to this view
I have a Django 'add business' view which adds a new business with an
I have Django project on Dreamhost server which has several views that returns Json
I have a django view that I want to return an Excel file. The
# settings.py EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' # view.py from django.core.mail import send_mail def send_letter(request): the_text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.