How to check for string in online text file in Python
Question:
How do I check for a string from an online text file? Right now, I am using urllib.request to read the data, but how would I check for a string from an online text file?
Answers:
I think that urllib perfectly matches your use case.
I do not understand why you opened the file when the text was already available in your variable, here’s a corrected version of your code, using an online txt file as per your request, available on www.w3.org website (you can clearly change the URL with whatever you prefer):
from urllib.request import urlopen
textpage = urlopen("https://www.w3.org/TR/PNG/iso_8859-1.txt")
text = str(textpage.read(), 'utf-8')
# Conditions
while True:
check_input = str(input("What do you want to search? "))
if check_input == "": # if no value is entered for the string
continue
if check_input in text: # string in present in the text file
print("Matched")
break
else: # string is absent in the text file
print("No such string found, try again")
continue
Output
What do you want to search? something
No such string found, try again
What do you want to search? SPACE
Matched
You can also use the requests library, here’s a another example:
#!/usr/bin/env python3
import requests as req
resp = req.get("https://www.w3.org/TR/PNG/iso_8859-1.txt")
text = resp.text
# Conditions
while True:
check_input = str(input("What do you want to search? "))
if check_input == "": # if no value is entered for the string
continue
if check_input in text: # string in present in the text file
print("Matched")
break
else: # string is absent in the text file
print("No such string found, try again")
continue
Output
What do you want to search? something
No such string found, try again
What do you want to search? SPACE
Matched
How do I check for a string from an online text file? Right now, I am using urllib.request to read the data, but how would I check for a string from an online text file?
I think that urllib perfectly matches your use case.
I do not understand why you opened the file when the text was already available in your variable, here’s a corrected version of your code, using an online txt file as per your request, available on www.w3.org website (you can clearly change the URL with whatever you prefer):
from urllib.request import urlopen
textpage = urlopen("https://www.w3.org/TR/PNG/iso_8859-1.txt")
text = str(textpage.read(), 'utf-8')
# Conditions
while True:
check_input = str(input("What do you want to search? "))
if check_input == "": # if no value is entered for the string
continue
if check_input in text: # string in present in the text file
print("Matched")
break
else: # string is absent in the text file
print("No such string found, try again")
continue
Output
What do you want to search? something
No such string found, try again
What do you want to search? SPACE
Matched
You can also use the requests library, here’s a another example:
#!/usr/bin/env python3
import requests as req
resp = req.get("https://www.w3.org/TR/PNG/iso_8859-1.txt")
text = resp.text
# Conditions
while True:
check_input = str(input("What do you want to search? "))
if check_input == "": # if no value is entered for the string
continue
if check_input in text: # string in present in the text file
print("Matched")
break
else: # string is absent in the text file
print("No such string found, try again")
continue
Output
What do you want to search? something
No such string found, try again
What do you want to search? SPACE
Matched