wwi
wwi

Reputation: 73

python search through text files composed of lines

if __name__ == '__main__':
    hid = '282'
    b = (hid)+"\n"
    hidtext = [open("foo.txt").readlines()]
    r = str (b) in hidtext
    print hidtext
    print r
    print (hid)
    print b

on above code i want to search for the "hid" value , however i am getting false on "r" although "282" does exist.

here is the output , foo is a text file with number in new lines.

/usr/bin/python2.7 /home/user/Dropbox/pycharm/win3.py 
[['282\n', '777\n', '418\n']] 
False 
282 
282
Process finished with exit code 0

Upvotes: 0

Views: 35

Answers (1)

Aaron
Aaron

Reputation: 2393

Remove the brackets outside the open("foo.txt").readlines()

if __name__ == '__main__':
    hid = '282'
    b = (hid)+"\n"
    hidtext = open("foo.txt").readlines()
    r = str (b) in hidtext
    print hidtext
    print r
    print (hid)
    print b

Result:

['282\n', '777\n', '418\n']
True
282
282

Upvotes: 1

Related Questions