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