viric@0: #!/usr/bin/python viric@0: # -*- coding: utf-8 -*- viric@0: viric@0: import shelve,string,os,MySQLdb,re viric@0: viric@0: database = 'lingvigilo' viric@0: dbuser = 'lingvigilouser' viric@0: dbpass = 'carrinclo' viric@0: dbtable = 'vortoj_ru_ca' viric@0: viric@0: db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,db=database) viric@0: viric@0: cursor = db.cursor() viric@0: viric@0: sourcefile = 'vortoj-ru-ca.txt' viric@0: viric@0: # Connexió base de dades viric@0: viric@0: viric@0: # Read txt viric@0: file = open(sourcefile, 'r') viric@0: viric@0: cursor.execute('DELETE FROM ' + dbtable); viric@0: viric@0: for line in file: viric@0: # Traiem l'INTRO final viric@0: line = line.rstrip('\n') viric@0: viric@0: # Arreglem cometes simples per a sql viric@0: line = re.sub(r"'",r"\'", line) viric@0: viric@0: # Separem per \t viric@0: words = string.split(line, '\t') viric@0: viric@0: print "Aldonante:", words[0], '=>', words[2] viric@0: viric@0: if words[1] == "verb": viric@0: #print ('INSERT INTO ' + dbtable + viric@0: # ' (vorto,tipo,traduko,param1,param2) '+ viric@0: # 'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' % viric@0: # (words[0], words[1], words[2], words[3], words[4])) viric@0: if (len(words) == 5): viric@0: cursor.execute('INSERT INTO ' + dbtable + viric@0: ' (vorto,tipo,traduko,param1,param2) '+ viric@0: 'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' % viric@0: (words[0], words[1], words[2], words[3], viric@0: words[4])) viric@0: else: viric@0: cursor.execute('INSERT INTO ' + dbtable + viric@0: ' (vorto,tipo,traduko,param1) '+ viric@0: 'VALUES(\'%s\',\'%s\',\'%s\',\'%s\');' % viric@0: (words[0], words[1], words[2], words[3])) viric@0: else: viric@0: #print ('INSERT INTO ' + dbtable + viric@0: # ' (vorto,tipo,traduko) '+ viric@0: # 'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1], viric@0: # words[2])) viric@0: cursor.execute('INSERT INTO ' + dbtable + viric@0: ' (vorto,tipo,traduko) '+ viric@0: 'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1], viric@0: words[2])) viric@0: viric@0: db.close() viric@0: viric@0: file.close()