txt2db.py
author viric <viriketo@gmail.com>
Sun, 13 Feb 2011 08:27:44 +0100
changeset 4 a3d29fb016c3
parent 0 6371497b4e53
permissions -rwxr-xr-x
Adding handling of tab and enter
viric@0
     1
#!/usr/bin/python
viric@0
     2
# -*- coding: utf-8 -*-
viric@0
     3
viric@0
     4
import shelve,string,os
viric@0
     5
viric@0
     6
viric@0
     7
sourcefile = 'vortoj-ru-ca.txt'
viric@0
     8
dbfile = 'vortoj-ru-ca.sdb'
viric@0
     9
try:
viric@0
    10
	os.remove(dbfile)
viric@0
    11
except:
viric@0
    12
	print "File '%s' doesn't exist. Creating..." % dbfile
viric@0
    13
else:
viric@0
    14
	print "File '%s' exist. Wipping..." % dbfile
viric@0
    15
viric@0
    16
dbase = shelve.open(dbfile)
viric@0
    17
viric@0
    18
# Read txt
viric@0
    19
file = open(sourcefile, 'r')
viric@0
    20
viric@0
    21
for line in file:
viric@0
    22
	line = line.rstrip('\n')
viric@0
    23
	words = string.split(line, '\t')
viric@0
    24
	print "Adding:", words[0], '=>', words[2]
viric@0
    25
	dbase[words[0]] = words[2]
viric@0
    26
viric@0
    27
file.close()
viric@0
    28
	
viric@0
    29
dbase.close()