Python: Checking any value in a list exists in a line of text

I’ve been doing some log file analysis to see what cypher queries were being run on a Neo4j instance and I wanted to narrow down the lines I looked at to only contain ones which had mutating operations i.e. those containing the words MERGE, DELETE, SET or CREATE

Here’s an example of the text file I was parsing:

$ cat blog.txt
MERGE (n:Person {name: 'Mark'}) RETURN n
MATCH (n:Person {name: 'Mark'}) ON MATCH SET n.counter = 1 RETURN n

So I only want lines 2 & 3 to be returned as the first one only returns data and doesn’t execute any updates on the graph.

I started off with a very crude way of doing this;

with open('blog.txt', 'r') as ins:
    for line in ins:
        if 'MERGE' in line or 'DELETE' in line or 'SET' in line or 'CREATE' in line:
           print line.strip()

A better way of doing this is to use the any command and make sure at least one of the words exists in the line:

mutating_commands = ['SET', 'DELETE', 'MERGE', 'CREATE']
with open('blog.txt', 'r') as ins:
    for line in ins:
        if any(command in line for command in mutating_commands):
           print line.strip()

I thought I might be able to simplify the code even further by using itertools but my best attempt so far is less legible than the above:

import itertools

commands = ['SET', 'CREATE', 'MERGE', 'DELETE']
with open('blog.txt', 'r') as ins:
    for line in ins:
        if len(list(itertools.ifilter(lambda x: x in line, mutating_commands))) > 0:
            print line.strip()

I think I’ll go with the 2nd approach for now but if I’m doing something wrong with itertools and it’s much easier to use than the example I’ve shown do correct me!

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Glenn Russell
Glenn Russell
9 years ago

If I understand your request and the examples correctly, you will only get line 3 with the word MERGE. None of the other lines contain the words you are looking for. Have I misunderstood what you are requesting to obtain?

Back to top button