Parse CSV Files in Python Django

As a Python developer, I’ve worked on importing and processing CSV data. Django, being a powerful web framework, makes it simple to handle CSV files efficiently. In this tutorial, I’ll walk you through how to parse CSV files in Django with clear examples you can use right away.

Whether you want to upload user data, product inventories, or any tabular data in CSV format, this guide will help you do it cleanly and effectively.

Let’s get in!

Parse CSV Files in Django

CSV (Comma-Separated Values) files are widely used for data exchange, especially in the USA, where businesses often export and import data in this format. Parsing CSV files allows you to read this data and use it in your Django models, making your web app dynamic and data-driven.

While Django doesn’t provide built-in CSV parsing tools, Python’s standard library and third-party packages have you covered. I’ll show you two practical methods to parse CSVs: using Python’s built-in csv module and using the popular pandas library.

Read: Create an API in Python Django

Method 1: Parse CSV Using Python’s Built-in csv Module

This is the most straightforward way to parse CSV files in Django. The csv module is part of Python’s standard library, so no extra installation is required.

Step 1: Create a Django Model

First, let’s assume you want to upload employee data from a CSV file. Here’s a simple model to store employee information:

from django.db import models

class Employee(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    department = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

Step 2: Create a Form to Upload CSV

You will need a form to upload the CSV file:

from django import forms

class UploadCSVForm(forms.Form):
    csv_file = forms.FileField()

Check out JWT Authentication Using Django Rest Framework

Step 3: Write the View to Parse CSV

Now, let’s write a Django view that reads the uploaded CSV file and saves the data to the database.

import csv
from django.shortcuts import render, redirect
from .models import Employee
from .forms import UploadCSVForm
from django.contrib import messages

def upload_csv(request):
    if request.method == 'POST':
        form = UploadCSVForm(request.POST, request.FILES)
        if form.is_valid():
            csv_file = request.FILES['csv_file']

            # Check if the uploaded file is a CSV
            if not csv_file.name.endswith('.csv'):
                messages.error(request, 'This is not a CSV file')
                return redirect('upload_csv')

            # Decode the file and read it using csv.reader
            file_data = csv_file.read().decode('utf-8').splitlines()
            reader = csv.reader(file_data)

            # Skip the header row
            next(reader)

            for row in reader:
                # Assuming CSV columns: first_name, last_name, email, department
                if len(row) != 4:
                    continue  # Skip rows that don't have exactly 4 columns

                first_name, last_name, email, department = row

                # Create or update employee record
                Employee.objects.update_or_create(
                    email=email,
                    defaults={
                        'first_name': first_name,
                        'last_name': last_name,
                        'department': department,
                    }
                )

            messages.success(request, 'CSV file has been uploaded and processed.')
            return redirect('upload_csv')
    else:
        form = UploadCSVForm()
    return render(request, 'upload_csv.html', {'form': form})

Step 4: Create the Template

Create a simple template upload_csv.html to upload your CSV file:

<!DOCTYPE html>
<html>
<head>
    <title>Upload CSV</title>
</head>
<body>
    <h2>Upload Employee CSV File</h2>
    {% if messages %}
        <ul>
            {% for message in messages %}
                <li>{{ message }}</li>
            {% endfor %}
        </ul>
    {% endif %}
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Upload</button>
    </form>
</body>
</html>

I executed the above example code and added the screenshot below.

django csv

Read File Upload in Django

Method 2: Parse CSV Using pandas

If you are dealing with large CSV files or require more complex data manipulation, pandas is a powerful library that simplifies CSV parsing.

Step 1: Install pandas

You can install pandas via pip:

pip install pandas

Step 2: Modify the View to Use pandas

Here’s how you can update the view to use pandas for parsing:

import pandas as pd
from django.shortcuts import render, redirect
from .models import Employee
from .forms import UploadCSVForm
from django.contrib import messages

def upload_csv_pandas(request):
    if request.method == 'POST':
        form = UploadCSVForm(request.POST, request.FILES)
        if form.is_valid():
            csv_file = request.FILES['csv_file']

            if not csv_file.name.endswith('.csv'):
                messages.error(request, 'This is not a CSV file')
                return redirect('upload_csv_pandas')

            # Read CSV file with pandas
            try:
                df = pd.read_csv(csv_file)
            except Exception as e:
                messages.error(request, f'Error reading CSV file: {e}')
                return redirect('upload_csv_pandas')

            # Check required columns
            expected_columns = {'first_name', 'last_name', 'email', 'department'}
            if not expected_columns.issubset(df.columns):
                messages.error(request, 'CSV file is missing required columns.')
                return redirect('upload_csv_pandas')

            for _, row in df.iterrows():
                Employee.objects.update_or_create(
                    email=row['email'],
                    defaults={
                        'first_name': row['first_name'],
                        'last_name': row['last_name'],
                        'department': row['department'],
                    }
                )

            messages.success(request, 'CSV file has been uploaded and processed using pandas.')
            return redirect('upload_csv_pandas')
    else:
        form = UploadCSVForm()
    return render(request, 'upload_csv.html', {'form': form})

I executed the above example code and added the screenshot below.

csv Django

Parsing CSV files in Django is a task you’ll frequently encounter, especially when working with business data in the USA. Using Python’s built-in csv module is quick and effective for simple CSV files, while pandas offers more flexibility and power for complex or large datasets.

Both methods integrate smoothly with Django’s ORM, making it easy to save imported data into your database. Choose the approach that best fits your project’s needs.

If you keep your CSV files clean and structured, parsing them will be hassle-free. Happy coding!

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.