-1

I am trying to match a string of text in a list with the in operator. All I get is a false response, even though the string is in the list.

[File ASpaceOdyssey.txt]

Behind every man now alive stand thirty ghosts, for that is the ratio by which the dead outnumber the living. Since the dawn of time, roughly a hundred billion human beings have walked the planet Earth.

Now this is an interesting number, for by a curious coincidence there are approximately a hundred billion stars in our local universe, the Milky Way. So for every man who has ever lived, in this Universe there shines a star.

'This day is not yet available'


But every one of those stars is a sun, often far more brilliant and glorious than the small, nearby star we call the Sun. And many - perhaps most - of those alien suns have planets circling them. So almost certainly there is enough land in the sky to give every member of the human species, back to the first ape-man, his own private, world-sized heaven - or hell.

My code:

Testdata = open('C:/ASpaceOdyssey.txt').readlines()  
'This day is not yet available' in Testdata

False

"'This day is not yet available'" in Testdata

False

"This day is not yet available" in Testdata

False
smci
  • 32,567
  • 20
  • 113
  • 146
  • 1
    Each line in the list returned by `readlines` ends with a newline (`\n`), so it won't match. – SuperStormer Jun 04 '21 at 23:38
  • See: [Difference between usage of “in” operator in strings and list containing strings in python](https://stackoverflow.com/questions/36201958/difference-between-usage-of-in-operator-in-strings-and-list-containing-strings) – smci Jun 04 '21 at 23:44
  • Does this answer your question? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – smci Jun 04 '21 at 23:45

2 Answers2

3

testdata is a list of strings, not a single string. But the way you test with the in operator is substring in string.

So you need a list-comprehension, to test each individual line:

[line for line in testdata if "This day is not yet available" in line]

or if you want the iterative version:

# result = []
for line in testdata:
    if "This day is not yet available" in line:
        # print(line) or result.append(line)
smci
  • 32,567
  • 20
  • 113
  • 146
  • I would only append `.strip()` or `.rstrip('\n')` to `line` to get rid of potential whitespace or newline characters. – Paul P Jun 05 '21 at 09:30
0

Try testing it out this way:

data = open('data.txt').readlines()
print(data)

print("\'This day is not yet available\'\n" in data)

At first I didn't include the newlines or the single quotes, but since I printed out the test data in the console after readlines() read it in, I got a clear view of what the 'in' statement would be acting on & was able to quickly correct the search.

jonesy
  • 3,502
  • 17
  • 23
  • Testdata = open('C:/Users/micha/source/ASpaceOdyssey.txt').readlines() print (Testdata) print("\'This day is not yet available'\'\n" in Testdata) Returns False – Searcher512 Jun 05 '21 at 02:17
  • Testdata = open('C:/Users/micha/source/ASpaceOdyssey.txt').readlines() print (Testdata) i = 0 for i in Testdata: print(i) t = 'This day is not yet available for booking of tee times' in Testdata print(t) False – Searcher512 Jun 05 '21 at 02:22
  • Thanks for the suggestions. I'm still looking – Searcher512 Jun 05 '21 at 02:24
  • Thank you all, I have solved my problem using your suggestions – Searcher512 Jun 05 '21 at 21:07