0

I am using a function to read a particular file, which in this case is options and do some regex for every line I read. The file I am reading is:

EXE_INC = \
    -I$(LIB_SRC)/me/bMesh/lnInclude \
    -I$(LIB_SRC)/mTools/lnInclude \
    -I$(LIB_SRC)/dynamicM/lnInclude

My code is

def libinclude():
    with open('options', 'r') as options:
    result = []
    for lines in options:
        if 'LIB_SRC' in lines and not 'mTools' in lines:
            lib_src_path = re.search(r'\s*-I\$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
            lib_path = lib_src_path.group(1).split()
            result.append(lib_path[0])
            print result
return (result)

Now as you can see, I look for the line that has mTools and filter using not 'mTools' in lines. However, when I have many such strings how do I filter? Say, for example, I would like to filter the lines that has mTools and dynamicM. Is it possible to put such strings in a list and then access the elements of that list against lines in the if statement?

2
  • use with open('options', 'r').readlines() Commented Feb 20, 2015 at 6:31
  • @latheefitzmeontv How does using readlines help me to filter out the lines? Thanks Commented Feb 22, 2015 at 21:59

1 Answer 1

1

Yes, you can use the built-in function all():

present = ['foo', 'bar', 'baz']
absent = ['spam', 'eggs']
for line in options:
    if all(opt in line for opt in present) and all(
           opt not in line for opt in absent):
       ...

See also: any().

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

1 Comment

Thanks @Lev. I was looking for this type of logic. I did look at any() and all() but I was doing something wrong with the logic within, which wasn't getting the right thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.