-
Notifications
You must be signed in to change notification settings - Fork 1
/
gender_lx_parser.py
34 lines (30 loc) · 966 Bytes
/
gender_lx_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time,MySQLdb
'''
txt to database converter
'''
def read_records(file_path,cursor):
counter=0
with open(file_path,"r") as txt_file:
for line in txt_file:
line=line.replace('"','').replace("'","").strip('\n')
data=line.split("$")
print data
sql="INSERT INTO gender_lx(GENDER_LX_ID,GENDER_CODE,GENDER_EN,GENDER_FR) VALUES('%d','%s','%s','%s')" %(int(data[0]),data[1],data[2],data[3])
cursor.execute(sql)
counter+=1
return counter
def database_connector():
db=MySQLdb.connect("localhost","root","MYNET696","medcare")
return db
def get_db_cursor(db):
return db.cursor()
def close_db(db):
db.close()
start_time=time.time()
file_path=input("File path: ")
db=database_connector()
cursor=get_db_cursor(db)
rec=read_records(file_path,cursor)
db.commit()
close_db(db)
print "Total %d records processed in %s seconds" % (rec,(time.time()-start_time))