dante
dante

Reputation: 97

print 5th word in Python

I am trying to find out the 5th word after a string of words from a SQL file.

string = INSERT INTO record

SQL file:

LOCK TABLES `zone1` WRITE;

/*!ALTER TABLE `zone1` DISABLE KEYS */;

/*!ALTER TABLE `zone1` ENABLE KEYS */;

UNLOCK TABLES;
--
-- 
--
LOCK TABLES `zone2` WRITE;

/*!ALTER TABLE `zone2` DISABLE KEYS */;

/*!ALTER TABLE `zone2` ENABLE KEYS */;

UNLOCK TABLES;

INSERT INTO record VALUES ('All', 'people', 'seems', 'to', 'need', 'data', 'processing'),('Randomly','words','picked','to','complete','the','sentence')

INSERT INTO record VALUES ('1', '2', '3', '4', '5', '6', '7')

INSERT INTO record VALUES ('8', '9', '10', '11', '12', '13', '14')

 LOCK TABLES `zone3` WRITE;

/*!ALTER TABLE `zone3` DISABLE KEYS */;

/*!ALTER TABLE `zone3` ENABLE KEYS */;

UNLOCK TABLES;

--
-- 
--
LOCK TABLES `zone4` WRITE;

/*!ALTER TABLE `zone4` DISABLE KEYS */;

/*!ALTER TABLE `zone4` ENABLE KEYS */;

UNLOCK TABLES;

Code

import re
import fileinput
import sqlite3

fd=open('dump.sql','r')
filesql=fd.read()
fd.close()

string='INSERT INTO record'

def words(line):
    return re.sub(r'[\(\)\';]', '', line.strip()).split()

with open('dump.sql','r') as dump:
    for line in dump:
        if string in line:
            tail=line.split(string)[1]
            for group in tail.split("),("):
                print words(tail)[3] 

When I am running this code, I get the following output:

seems,seems,3,10,

I was expecting to get:

seems,picked,3,10,

Can somebody hint me what is wrong in my code?

Thank you in advance, Dan

Upvotes: 0

Views: 274

Answers (3)

PythonProgrammi
PythonProgrammi

Reputation: 23443

Try this

I don't know if it will work for all your sql file...

string = 'INSERT INTO record VALUES'

with open('dump.sql', 'r') as dump:
    for line in dump:
        if string in line:
            tail = line.split(string)[1]
            for group in tail.split("),("):
                group = group.replace("\'", "").replace("(", "").replace(")\n", "")
                print(group.split(",")[2].strip())

output:

seems
picked
3
10
>>> 

Get the results in a list

In case you want to get the results in a list

string = 'INSERT INTO record VALUES'
l = []
with open('dump.sql', 'r') as dump:
    for line in dump:
        if string in line:
            tail = line.split(string)[1]
            for group in tail.split("),("):
                group = group.replace("\'", "").replace("(", "").replace(")\n", "")
                print(group.split(",")[2].strip())
                l.append(group.split(",")[2].strip())

Upvotes: 0

Tenzin
Tenzin

Reputation: 2505

You could calculate the start area of the string, and then print everything behind it.
For example:

stringA = "Hello World My name is Robot Tim, and who are you?"

# Find the character position to start.
n = stringA.index(stringA.split()[5]);
#print(n);

stringB = stringA[n:]
print(stringB);

Upvotes: 0

mrCarnivore
mrCarnivore

Reputation: 5078

This regex works

import re

with open('dump.sql', 'r') as f:
    s = f.read()

matches = re.findall('\((\'[^\']*\',\s*){3}', s)
for match in matches:
    print(match)

Upvotes: 1

Related Questions