<< March 2010 | Home | May 2010 >>

Python - CSV to sqllite

Nice Python CSV to sqlite conversion...

# toSqlite.py - Imports humod06.txt to sqlite.

import csv
import sqlite3

conn = sqlite3.connect('humod.sqlite')
c = conn.cursor()

c.execute('''DROP TABLE IF EXISTS skl_desc''')

c.execute('''CREATE TABLE skl_desc (
id INTEGER PRIMARY KEY AUTOINCREMENT,
parent INTEGER,
mod INTEGER,
desc TEXT,
abr TEXT,
r INTEGER)
''')

insertSql = '''
INSERT INTO skl_desc
VALUES (null, ?, ?, ?, ?, ?)
'''

reader = csv.reader(open('humod06.txt', 'rb'), delimiter='\t')
rownum = 0
for row in reader:
    if rownum == 0: pass
    else:
        parent = 0
        rValue = 0
        if row[5] == 'R':
            rValue = 1
        t = (parent,
            row[1],
            unicode(row[4].strip(), 'utf8'),
            unicode(row[2].strip(), 'utf8'),
            rValue)
        c.execute(insertSql, t)
    rownum += 1
print('rows: %s' % rownum)
conn.commit()
c.close()
Tags : ,
Social Bookmarks :  Add this post to Slashdot    Add this post to Digg    Add this post to Reddit    Add this post to Delicious    Add this post to Stumble it    Add this post to Google    Add this post to Technorati    Add this post to Bloglines    Add this post to Facebook    Add this post to Furl    Add this post to Windows Live    Add this post to Yahoo!

Export this post as PDF document  Export this post to PDF document

Python - CSV to XML

# toXml.py - Imports humod06.txt to XML.

import csv
from xml.dom.minidom import parseString

doc = parseString('<skill_descriptors lvl="0"/>')

reader = csv.reader(open('humod06.txt', 'rb'), delimiter='\t')
rownum = 0
elm = doc.documentElement
for row in reader:
    if rownum == 0:
        header = row
    else:
        sd = doc.createElement('skill_descriptor')
        colnum = 0
        for col in row:
            #print '%-8s: %s' % (header[colnum], col)
            sd.setAttribute(header[colnum].lower(), unicode(col.strip(), 'utf8'))
            colnum += 1

        while row[3] <= elm.getAttribute('lvl'):
            elm = elm.parentNode
        elm.appendChild(sd)
        elm = sd

    rownum += 1
print(doc.toxml())
print('rows: %s' % rownum)
doc.unlink()
Tags : ,
Social Bookmarks :  Add this post to Slashdot    Add this post to Digg    Add this post to Reddit    Add this post to Delicious    Add this post to Stumble it    Add this post to Google    Add this post to Technorati    Add this post to Bloglines    Add this post to Facebook    Add this post to Furl    Add this post to Windows Live    Add this post to Yahoo!

Export this post as PDF document  Export this post to PDF document