0

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.

4
  • if hobby is dancing and singing Commented Jul 13, 2014 at 12:21
  • Please modify my entire program,my program its getting worser after every editing. Commented Jul 13, 2014 at 12:21
  • two things are involved in your code 1.) regex 2.) database Commented Jul 13, 2014 at 12:31
  • what about if name include first name last name. I have added that part also in my post. Commented Jul 13, 2014 at 12:38

3 Answers 3

2

You are mixing SQL parameters with string formatting, and that doesn't work. Pass in the parameters as a separate argument:

cursor.execute('''INSERT into Details (Names, Hobby)
                  values (%s, %s)''', (name, hobby))
db.commit()

You need to use %s for the placeholders when using the MySQLdb database adapter, and you need to commit the transaction as well.

Your patterns setup is not valid Python; if you wanted to match multiple patterns, then make that a proper list:

patterns = (
    re.compile('My name is (\w+) and my hobby is (\w+)\.', re.IGNORECASE),
    re.compile('Me (\w+) and my interest is (\w+)\.', re.IGNORECASE),
)

and just loop over these patterns until one matches:

for pattern in patterns:
     match_result = pattern.match(line)
     if match_result:
         name, hobby = match_result.groups()

Demo:

>>> import re
>>> patterns = (
...     re.compile('My name is (\w+) and my hobby is (\w+)\.', re.IGNORECASE),
...     re.compile('Me (\w+) and my interest is (\w+)\.', re.IGNORECASE),
... )
>>> lines = '''\
... 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.
... '''.splitlines()
>>> for line in lines:
...     for pattern in patterns:
...         match_result = pattern.match(line)
...         if match_result:
...             name, hobby = match_result.groups()
...             print(name, hobby)
... 
('Casssandra', 'Cooking')
('Archana', 'Playing')
('Leela', 'Baking')

All put together that becomes:

import MySQLdb
import re

patterns = (
    re.compile('My name is (\w+) and my hobby is (\w+)\.', re.IGNORECASE),
    re.compile('Me (\w+) and my interest is (\w+)\.', re.IGNORECASE),
)
db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="root", # your username
                      passwd="mysql", # your password
                      db="sakila") # name of the data base

with open('qwer2.txt','r') as file, db as cursor:
    for line in file:
        for pattern in patterns:
             match_result = pattern.match(line)
             if match_result:
                 name, hobby = match_result.groups()
                 cursor.execute(
                     '''INSERT INTO Details (Names, Hobby)
                        VALUES (%s, %s)''',
                     (name, hobby))
                 break

This also uses the database connection as a context manager (which gives you a cursor), this auto-commits the changes when the with block is done without errors.

Sign up to request clarification or add additional context in comments.

12 Comments

@adarshram: then you matched a name that doesn't fit the database column constraint. Create a bigger column or skip such names.
My name match the database column and what is meant by create bigger column/skip such names?I mean for insert it just creates so what is need for making column bigger?
@adarshram: your table schema has constraints for each column. Say it is a VARCHAR(100) column, then you can only fit names of up to 100 characters in that column. You tried to insert a name that was bigger than that limit.
Works great!As i metioned its a paragraph,so many names are not taken,but it takes perfect from individual lines.But it doenot take from a sentence eg:"Me Leela and my interest is Baking.My name is John and my interest is Gaming."It takes leela and hobby but not John and Hobby.Thats the problem?Is there a way to solve that?
@adarshram: That's because there are two sentences on one line; re.search() will only find the first, not all matches. Use re.findall() then and loop over all the results. However, you cannot keep asking new questions in comments; this doesn't really scale. If you continue to struggle with that, ask a new question.
|
2

Get the matched group from index 2 and 4.

/(?:(My Name is|Me) )(.*?) and (?:my (Hobby|interest) is )([^\\.]*)./i

Online demo

sample code:

import re
p = re.compile(ur'(?:(My Name is|Me) )(.*?) and (?:my (Hobby|interest) is )([^\\.]*).', re.IGNORECASE)
test_str = u"..."

re.findall(p, test_str)

3 Comments

first extract the required data from line using above regex that matches singing and dancing as well. inserting in database is separate task.
first do code for extracting the vales then extend your code to insert it in database. I have no idea about database insertion code.
could u provide a sample of extracted output after using ur code?
0

You've defined the cursor correctly and the only thing you need to do is to execute SQL statements which you can do it by calling execute on cursor object. Also, I recommend you to checkout the MySQLDB documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.