vortoj2mysql
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,MySQLdb,re
viric@0
     5
viric@0
     6
database = 'lingvigilo'
viric@0
     7
dbuser = 'lingvigilouser'
viric@0
     8
dbpass = 'carrinclo'
viric@0
     9
dbtable = 'vortoj_ru_ca'
viric@0
    10
viric@0
    11
db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,db=database)
viric@0
    12
viric@0
    13
cursor = db.cursor()
viric@0
    14
viric@0
    15
sourcefile = 'vortoj-ru-ca.txt'
viric@0
    16
viric@0
    17
# Connexió base de dades
viric@0
    18
viric@0
    19
viric@0
    20
# Read txt
viric@0
    21
file = open(sourcefile, 'r')
viric@0
    22
viric@0
    23
cursor.execute('DELETE FROM ' + dbtable);
viric@0
    24
viric@0
    25
for line in file:
viric@0
    26
	# Traiem l'INTRO final
viric@0
    27
	line = line.rstrip('\n')
viric@0
    28
viric@0
    29
	# Arreglem cometes simples per a sql
viric@0
    30
	line = re.sub(r"'",r"\'", line)
viric@0
    31
viric@0
    32
	# Separem per \t
viric@0
    33
	words = string.split(line, '\t')
viric@0
    34
viric@0
    35
	print "Aldonante:", words[0], '=>', words[2]
viric@0
    36
viric@0
    37
	if words[1] == "verb":
viric@0
    38
		#print ('INSERT INTO ' + dbtable +
viric@0
    39
		#	' (vorto,tipo,traduko,param1,param2) '+
viric@0
    40
		#	'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' %
viric@0
    41
		#	(words[0], words[1], words[2], words[3], words[4]))
viric@0
    42
		if (len(words) == 5):
viric@0
    43
			cursor.execute('INSERT INTO ' + dbtable +
viric@0
    44
				' (vorto,tipo,traduko,param1,param2) '+
viric@0
    45
				'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' %
viric@0
    46
				(words[0], words[1], words[2], words[3],
viric@0
    47
				words[4]))
viric@0
    48
		else:
viric@0
    49
			cursor.execute('INSERT INTO ' + dbtable +
viric@0
    50
				' (vorto,tipo,traduko,param1) '+
viric@0
    51
				'VALUES(\'%s\',\'%s\',\'%s\',\'%s\');' %
viric@0
    52
				(words[0], words[1], words[2], words[3]))
viric@0
    53
	else:
viric@0
    54
		#print ('INSERT INTO ' + dbtable +
viric@0
    55
		#	' (vorto,tipo,traduko) '+
viric@0
    56
		#	'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1],
viric@0
    57
		#	words[2]))
viric@0
    58
		cursor.execute('INSERT INTO ' + dbtable +
viric@0
    59
			' (vorto,tipo,traduko) '+
viric@0
    60
			'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1],
viric@0
    61
			words[2]))
viric@0
    62
viric@0
    63
db.close()
viric@0
    64
viric@0
    65
file.close()