Showing posts with label csv parser in django. Show all posts
Showing posts with label csv parser in django. Show all posts

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.

How to Display Comma Seperated File in Table using django Python

Common task for showing data could be to load a comma/Tab or any other character seperated file and display in table on webpage. this Could be achieved by simple Process. First Step is to design Template.
Here is html code with django template tags to be used as template. Remember here I have used Simple html and django template tags which will be rendered at the time of execution. if you need any information on how these tags works let you ask them in comments.

{tbr.html}

<html>
<head>
<title>
My Test Django App
</title>
</head>
<body>
<table style = "width:600px;background-color:#888888;" border = "2">
        <tr>
         <td>Header1</td>
         <td>Code</td>
         <td>Vendor</td>
         <td>Destination</td>
         <td>Rate</td>
         <td>Effective Date</td>
        </tr>
        {% for line in lines %}
        <tr>
         {% for rec in line %}
         <td>{{rec}}</td>
         {% endfor %}
        </tr>
        {% endfor %}
       </table>
</body>
</html>

Then in View.py Render From function called by URL and Define another function to read file.

from django.core.context_processors import csrf
from django.http import HttpResponse
from django.template import Template, loader, Context
import os;

def index(request):
   lines = ReadTBR()
   t = loader.get_template('Templates\\tbr.html')
   c = Context({"user":request.user, "lines":lines})
   c.update(csrf(request))
   return HttpResponse(t.render(c))
def ReadTBR():
  path = os.path.join('.','../tbr.out')
  tbr = open(path,"r")
  res = []
  for line in tbr.readlines():
    l = line.split('\t')
    rec = [l[0],l[1],l[2],l[3],l[4],l[5]]
    res.append(rec)
  return res