frazoj2mysql
author viric <viriketo@gmail.com>
Mon, 03 Sep 2012 23:35:31 +0200
branchgo
changeset 7 d5750f4e6e8c
parent 0 6371497b4e53
permissions -rwxr-xr-x
Adding a first attempt at go code
viric@0
     1
#!/usr/bin/python
viric@0
     2
# -*- coding: utf-8 -*-
viric@0
     3
viric@0
     4
import sys,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 = 'frazoj_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
if (len(sys.argv) != 2):
viric@0
    16
	print "Diru nomon de dosiero."
viric@0
    17
	sys.exit()
viric@0
    18
else:
viric@0
    19
	sourcefile=sys.argv[1]
viric@0
    20
viric@0
    21
viric@0
    22
# Read txt
viric@0
    23
file = open(sourcefile, 'r')
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[1]
viric@0
    36
viric@0
    37
	cursor.execute('INSERT INTO ' + dbtable +
viric@0
    38
		' (vorto,frazo) '+
viric@0
    39
		'VALUES(\'%s\',\'%s\');' % (words[0], words[1]))
viric@0
    40
viric@0
    41
db.close()
viric@0
    42
viric@0
    43
file.close()