0

i have a python script that simply get file content from a specific URL and show the content using print command.

This is what actually i done:

from urllib.request import urlopen
import urllib

url = "http://www.mywebsite/file.txt"

req = urllib.request.Request(url)
with urllib.request.urlopen(req) as f:
    resp = f.read().decode('utf-8')
    print(resp)

When i print resp i see correctly the content of the file. Now, i would like to iterate line by line the content or resp in order to perform some string check for each row iterated.

Can you help me please? thank in advance

xXJohnRamboXx
  • 739
  • 3
  • 10
  • 24
  • 1
    See the duplicat, which addresses Python 2, but applies to `urllib.request` just as much. Read your data in chunks. – Martijn Pieters Feb 07 '18 at 15:43
  • 1
    Or see [Iterate over the lines of a string](https://stackoverflow.com/questions/3054604/iterate-over-the-lines-of-a-string) if you're not concerned about reading the file in slowly but just want to split the string into lines. – glibdud Feb 07 '18 at 15:44
  • @glibdud: why do that? You can just loop over the response object, iteration yields lines. – Martijn Pieters Feb 07 '18 at 15:52
  • @MartijnPieters It wasn't clear the asker knew how to do either... consider it a chance to learn two things with one question. – glibdud Feb 07 '18 at 15:58

0 Answers0