Home > Software design >  how do I append multiple lines from a url into a list?
how do I append multiple lines from a url into a list?

Time:12-03

I am trying to read a url which contains words on each line, and I want to take each line from the url and append it to my list. With the code I have, it only appends the first line from the url and then stops. I thought that my for-loop would solve this problem, but it doesn't. I've put my code below, which also includes the url I am referring to.

import urllib.request

listOfStrings = []
try:
   with urllib.request.urlopen('https://www.cs.queensu.ca/home/cords2/bingo.txt') as f:
       for line in f:
           d = f.read().decode('utf-8')
           split = d.split("\n")
           listOfStrings.append(line)
except urllib.error.URLError as e:
    print(e.reason)
print(listOfStrings)

CodePudding user response:

I believe what you are looking for is actually contained within the value split. When you use .split on a string it returns a list of strings split by the separator you have chosen, in this case \n. By removing the individual line reads you capture all the values:

with urllib.request.urlopen('https://www.cs.queensu.ca/home/cords2/bingo.txt') as f:
   d = f.read().decode('utf-8')
   split= d.split("\n") 
print(split) 

Returns: ['motorcycle', 'police car', 'fire truck', 'ambulance', 'military vehicle', ...]

CodePudding user response:

You should use readlines rather than the whole file:

import urllib.request

try:
    with urllib.request.urlopen('https://www.cs.queensu.ca/home/cords2/bingo.txt') as f:
        list_of_items = f.readlines()
except urllib.error.URLError as e:
    print(e.reason)
print(list_of_items)

f is a file pointer, and one of the methods on the file pointer type is readlines(). This gives you each line (delimited by the \n) without you having to manually parse it. In this case that works fine, and you can simply use the output from that.

We can see this in action in the REPL:

>>> with urllib.request.urlopen('https://www.cs.queensu.ca/home/cords2/bingo.txt') as f:
...     list_of_items = f.readlines()
... 
>>> list_of_items
[b'motorcycle\n', b'police car\n', b'fire truck\n', b'ambulance\n', b'military vehicle\n', b'bus\n', b'airport shuttle\n', b'limousine\n', b'Hummer\n', b'Mini-Cooper\n', b'PT Cruiser\n', b'Austin Healey\n', b'convertible\n', b'electric car\n', b'bicycle\n', b'cows\n', b'stop sign\n', b'camper\n', b'taxi\n', b'pizza delivery\n', b'tow truck\n', b'dump truck\n', b'cemetary\n', b'logging truck\n', b'mattress on roof\n', b'sailboat\n', b'hotel\n', b'willow tree\n', b'blue van\n', b'flying pig\n', b'sports team bumper sticker\n', b'Canadian flag\n', b'service center\n', b'dog in car\n', b'someone stopped by police\n', b'for sale sign\n', b'revolving sign\n', b'birds on a wire\n', b'field of flowers\n', b'clothesline\n', b'traffic cone\n', b'a shoe on the road\n', b'Tim Hortons\n', b'baseball game\n', b'airplane\n', b'license plate starting with A\n', b'Quebec license plate\n', b'US license plate\n', b'yeild sign\n', b'construction sign\n', b'baby on board sign\n', b'church\n', b'barn\n', b'hawk\n']
  • Related