Thursday 15 May 2014

c# - Searching for substrings in file.readlines() in python -



c# - Searching for substrings in file.readlines() in python -

just starting python, excuse me if sound utterly thick.

assuming next input: my_file content:

we love unicorns love beer love free (an in free beer)

i have expected next homecoming true:

# my_file = path valid file open(my_file) f: lines = f.readlines() if 'beer' in lines: print("found beer") # not happen

or used c#'s way, after i'll have matching lines:

// assuming i've done similar lines = open , read file var v = line in lines line.contains("beer") select line;

what pythonian equivalent fetching lines hold beer example?

you close, need check substring in each line, not in list of lines.

with open(my_file) f: line in f: if 'beer' in line: print("found beer")

as example,

lines = ['this line', 'this sec line', 'this 1 has beer']

this first case trying do

>>> 'beer' in lines false

this code showed above do

>>> line in lines: print('beer' in line) false false true

c# python string search

No comments:

Post a Comment