How to read a text file from a URL in Python

Опубликовано: 24 Октябрь 2024
на канале: Programmer Banda
4,531
132

How to read a text file from a URL in Python

urllib.request.urlopen(URL) to access the text at the given URL. Iterate through each line of the text and decode the line to a readable format using line.decode("utf-8").

import urllib.request
url = "http://textfiles.com/adventure/advent..."
file = urllib.request.urlopen(url)

for line in file:
decoded_line = line.decode("utf-8").strip()
print(decoded_line)




#python #urllib #file #python3 #pythonprogramming #learnpython #short #youtubeshorts