user2985124
user2985124

Reputation: 63

Python script finding text in a file and doing checks on it

I have a file called parks, I read it and save it in parks_f. Now I want it to find a string in this file and print everything until the next ',' and do some checks on it

parks_f = open(parks).read()

find_str = "text_to_be_found=" in parks_f

if (find_str == (global_var_P) or find_str == (string_const))

       # do this

else

       #do this

How can I do this in python?

Upvotes: 1

Views: 41

Answers (1)

Vivek Anand
Vivek Anand

Reputation: 651

with open('input.txt', 'r') as f:
    x = f.read()
    x = x.split(',')
    print x

The above code will open a file and perform read operation over it. In python the file read() returns a string. In the next line, i have split the returned string by using a symbol which is ',' here. So, after execution of that statement, x is now a list. You can do any thing with that list as desired, same as any other python list.

Upvotes: 1

Related Questions