I am coding to find names and hobby from text file and store it in Details(mysql Table).The details table consists of 'Names' and 'Hobbies'.I cant able to store to my database.
import MySQLdb
import re
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="mysql", # your password
db="sakila") # name of the data base
cursor = db.cursor()
with open('qwer2.txt','r') as file:
for line in file:
patterns = [
a,b= re.compile('My name is (\w+) and my hobby is (\w+)\.', re.IGNORECASE),
a,b= re.compile('Me (\w+) and my interest is (\w+)\.', re.IGNORECASE),
]
match_result = patterns[0].match(line) or patterns[1].match(line)
name, hobby = match_result.groups()
cursor.execute('''INSERT into Details (Names, Hobby)
values (? , ?)'''%(a,b)
My text file is a paragraph:
My Name is Casssandra and my Hobby is Cooking.
My name is Archana and my hobby is Playing.Me Adarsh and my interest is Programming.
Me Leela and my interest is Baking.My name is John and my interest is Gaming.
Output:
Names | Hobby
Cassandra Cooking
Archana Playing
Adarsh Programming
Leela Baking
John Gaming
Please help me rectify my program to store into the table.
dancing and singing