1

I want to take the first line of a file opened from an url, search for a specific string and then split that string.

    request=urllib.request.Request(url)
    response=urllib.request.urlopen(request)
    input_file=response.readlines()
    for l in input_file:
        if "target" in l:
           dum, stat = l.split(":")
           stat = stat.strip()

I expect to get a stat="StationX" instead I get

TypeError: a bytes-like object is required, not 'str'

because input_file is a list of type bytes instead of type strings. I don't know how to either bring input_file in as strings (I thought thats what readlines() vs read() did?) or convert the list of type bytes to a list of type stings.

Skam
  • 7,298
  • 4
  • 22
  • 31
Chloe
  • 19
  • 2
  • 1
    The [requests](https://2.python-requests.org/en/master/) package in PyPI could be very helpful here. – Skam Aug 11 '19 at 20:37

1 Answers1

1

The urllib.request package has a little nuance to it as highlighted below. One might expected the return type of .read() to be a string but it's actually raw bytes that you have to decode.

>>> import urllib.request
>>> req = urllib.request.Request("http://www.voidspace.org.uk")
>>> res = urllib.request.urlopen(req)
>>> raw_contents = res.read()
>>> type(raw_contents)
<class 'bytes'>
>>> page = raw_contents.decode()
>>> type(page)
<class 'str'>

Now in your case

request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
raw_lines = response.readlines()
for raw_line raw_lines:
    line = raw_line.decode()
    if "target" in line:
       dum, stat = l.split(":")
       stat = stat.strip()

Alternatively,

for line in map(lambda x: x.decode(), raw_lines):
    # etc
Skam
  • 7,298
  • 4
  • 22
  • 31
  • this makes a string out of every character in the file not a string for each line in the file. – Chloe Aug 11 '19 at 21:14