Reputation: 571
I'm trying to follow the 'Earthquakes' example from this tutorial. The code is as follows:
import urllib
from mpl_toolkits.basemap import Basemap
# Significant earthquakes in the last 30 days
url = urllib.request.urlopen("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.csv")
# Reading and storage of data
data = url.read()
data = data.split(b'\n')[+1:-1]
E = np.zeros(len(data), dtype=[('position', float, 2),
('magnitude', float, 1)])
for i in range(len(data)):
row = data[i].split(',')
E['position'][i] = float(row[2]),float(row[1])
E['magnitude'][i] = float(row[4])
I get the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-66-eed77a8fb64a> in <module>()
16
17 for i in range(len(data)):
---> 18 row = data[i].split(',')
19 E['position'][i] = float(row[2]),float(row[1])
20 E['magnitude'][i] = float(row[4])
TypeError: a bytes-like object is required, not 'str'
I googled similar questions and as far as I understand, it has to do with using Python 3.+ instead of 2.7, but I couldn't figure out how to adjust the code for it to work. Sorry if the question is dumb, I don't understand what I need to do.
I tried with the following but to no avail:
if sys.version_info[0] == 3:
from urllib.request import urlopen
else:
from urllib import urlopen
with urlopen(url) as url:
data = url.read()
Upvotes: 1
Views: 160
Reputation: 26916
When you try to use split
you are using ','
instead of b','
. The point is that bytes
and str
need to be explicitly converted into each other using encode
or decode
, or you have to be consistently using one or the other.
Upvotes: 2