vortoj2mysql
changeset 0 6371497b4e53
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vortoj2mysql	Thu May 18 22:59:47 2006 +0200
@@ -0,0 +1,65 @@
+#!/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()