vortoj2mysql
author viric@llimona
Thu, 18 May 2006 22:59:47 +0200
changeset 0 6371497b4e53
permissions -rwxr-xr-x
Init from svn. The wxpython is the final one.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import shelve,string,os,MySQLdb,re

database = 'lingvigilo'
dbuser = 'lingvigilouser'
dbpass = 'carrinclo'
dbtable = 'vortoj_ru_ca'

db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,db=database)

cursor = db.cursor()

sourcefile = 'vortoj-ru-ca.txt'

# Connexió base de dades


# Read txt
file = open(sourcefile, 'r')

cursor.execute('DELETE FROM ' + dbtable);

for line in file:
	# Traiem l'INTRO final
	line = line.rstrip('\n')

	# Arreglem cometes simples per a sql
	line = re.sub(r"'",r"\'", line)

	# Separem per \t
	words = string.split(line, '\t')

	print "Aldonante:", words[0], '=>', words[2]

	if words[1] == "verb":
		#print ('INSERT INTO ' + dbtable +
		#	' (vorto,tipo,traduko,param1,param2) '+
		#	'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' %
		#	(words[0], words[1], words[2], words[3], words[4]))
		if (len(words) == 5):
			cursor.execute('INSERT INTO ' + dbtable +
				' (vorto,tipo,traduko,param1,param2) '+
				'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' %
				(words[0], words[1], words[2], words[3],
				words[4]))
		else:
			cursor.execute('INSERT INTO ' + dbtable +
				' (vorto,tipo,traduko,param1) '+
				'VALUES(\'%s\',\'%s\',\'%s\',\'%s\');' %
				(words[0], words[1], words[2], words[3]))
	else:
		#print ('INSERT INTO ' + dbtable +
		#	' (vorto,tipo,traduko) '+
		#	'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1],
		#	words[2]))
		cursor.execute('INSERT INTO ' + dbtable +
			' (vorto,tipo,traduko) '+
			'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1],
			words[2]))

db.close()

file.close()