frazoj2mysql
author viric <viriketo@gmail.com>
Sun, 13 Feb 2011 08:27:44 +0100
changeset 4 a3d29fb016c3
parent 0 6371497b4e53
permissions -rwxr-xr-x
Adding handling of tab and enter

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

import sys,string,os,MySQLdb,re

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

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

cursor = db.cursor()

if (len(sys.argv) != 2):
	print "Diru nomon de dosiero."
	sys.exit()
else:
	sourcefile=sys.argv[1]


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

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[1]

	cursor.execute('INSERT INTO ' + dbtable +
		' (vorto,frazo) '+
		'VALUES(\'%s\',\'%s\');' % (words[0], words[1]))

db.close()

file.close()