iterable 与 iterator / bytestream

iterable 与 iterator

range returns an iterable, not an iterator. To get an iterator, you need to call iter()

>>> x = range(20)
>>> next(x)

TypeError: 'range' object is not an iterator


>>> x = (i for i in range(30))
>>> next(x)
0

urllib has no attribute request

Use this:

import urllib.request

The reason is:

With packages, like this, you sometimes need to explicitly import the piece you want. That way, the urllib module doesn't have to load everything up just because you wanted one small part.

bytestream encode to string

urllib.request.urlopen() returns a bytestream

response = urllib.request.urlopen(an_url)
output = response.decode('utf-8')
posted @ 2020-07-29 17:18  friedCoder  阅读(112)  评论(0编辑  收藏  举报