I am a novice when it comes to computer science. In my class we are working on a project dealing with earthquake data and text files, specifically from this site:
http://www.choongsoo.info/teach/mcs177-sp12/projects/earthquake/earthquakeData-02-23-2012.txt
Here is the info from the project.
Write the contract, docstring, and implementation for a procedure parseEarthquakeData that takes two dates in the format YYYY/MM/DD, accesses the earthquake data from the above USGS URL and returns a list of lists of four numbers representing latitude, longitude, magnitude and depth. The outer list should contain one of these four-number lists for each earthquake between the given dates.
NOTE: If you have the first-edition textbook, it has two errors that were corrected in the second-edition. First, the first-edition textbook has a typo: use urllib.request instead of urllib. Second, remember that you need to decode anything you read from the web as ASCII. For example, if you read a string into a variable myString, you can decode it by:
decodedString = myString.decode('ascii')
What I have so far is:
import urllib
#
def parseEarthquakeData(date1,date2):
quakeFile=urllib.urlopen('http://www.choongsoo.info/teach/mcs177-sp12/projects/earthquake/earthquakeData-02-23-2012.txt')
latitude=[]
longitude=[]
magnitude=[]
depth=[]
for i in quakeFile:
i=i.decode('ascii')
splitData=i.split(',')
if betweenDates(splitData[0],date1,date2):
latitude.append(splitData[2])
longitude.append(splitData[3])
magnitude.append(splitData[4])
depth.append(splitData[5])
totalList=[]
totalList.append(latitude)
totalList.append(longitutde)
totalList.append(magnitude)
totalList.append(depth)
return totalList
and when I enter the function into the python shell I get this:
parseEarthquakeData("2012/02/23","2012/02/22")
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
parseEarthquakeData("2012/02/23","2012/02/22")
File "/Users/BaronRitic/Desktop/Python/Project 6/betweenDates.py", line 17, in parseEarthquakeData
quakeFile=urllib.urlopen('http://www.choongsoo.info/teach/mcs177-sp12/projects/earthquake/earthquakeData-02-23-2012.txt')
AttributeError: module 'urllib' has no attribute 'urlopen'
I have Python version 3.5.1 I'm still relatively new to all of the terms and processes. I'm not entirely sure how to use the urllib module. Any help will be greatly appreciated!
PS the contract is empty because I usually do that last.
It looks like you might have glanced at some Python 2.7 examples before writing your code?
In Python 3, the urlopen function is not inside urllib, it is inside urllib.request
. So the first line in your script should be:
from urllib.request import urlopen
And then simply use urlopen
below.
However, if your class uses Python 2 for its code examples, you are probably better off installing Python 2.7 (but tell your instructor to please switch to Python 3!).