Saturday, 12 November 2011

CSV Parser and Database

Here is how we can parse values from csv file and save records to database. inorder to do this first you need to define table structure in models.py.

Here is a sample Model:

from django.db import models
   
class Code(models.Model):
    code = models.CharField(max_length = 20)
    description = models.CharField(max_length = 40)

from django.http import HttpResponse
from django.template import Context,loader
from Code.models import Code
from django.shortcuts import render_to_response
import csv
import os

def readfile(request):
    func()
    t = loader.get_template('readfile.html')
    c = Context({"user":request.user,"entries":Code.objects.all()})
    return HttpResponse(t.render(c))


# reads-in the data from file
# looks for file in the given directory
# identifies file as having .csv extensions
# opening the file using CSV library
# reads the records line-by-line and save into database
def func():
    files = os.path.join('.','Media/Uploads')
    for f in os.listdir(files):
        fname = f.split('.')
        name , extantion = fname[0], fname[-1]
        if extantion == 'csv':
            csv_filepathname = os.path.join(files,f)
            dataReader = csv.reader(open(csv_filepathname), delimiter=',',quotechar='|')
            for line in dataReader:
                cc = Code()
                cc.description = line[0]
                cc.code = line[1]
                cc.save()
Best of Luck.

No comments:

Post a Comment