In Python Please, I am using Python 3, Wing 3. Write a program that reads earthq
ID: 3591149 • Letter: I
Question
In Python Please, I am using Python 3, Wing
3. Write a program that reads earthquake information from a text file (included with powerpoint on moodle) and prints the number of earthquakes with a magnitude greater than or equal to 4.0.
Note: the file is delimited with tabs
time latitude longitude depth mag id
2016-10-14T18:29:03.610Z 9.1379 125.623 34.97 5.4 us20007e6x
2016-10-14T18:09:33.930Z -21.8732 -68.4961 118.09 4.7 us20007e6r
2016-10-14T17:25:24.190Z 16.7085 -60.6958 25.69 4.4 us20007e6i
2016-10-14T17:23:20.980Z 36.7393 71.162 191.26 4.4 us20007e6h
2016-10-14T17:22:45.000Z 56.5134 -155.0895 0 2.8 ak14205873
2016-10-14T16:46:15.000Z 60.0135 -151.2165 45.6 2.5 ak14205865
2016-10-14T14:06:38.200Z 17.9097 -65.6181 6 3 pr16288005
2016-10-14T12:59:05.000Z 59.8312 -151.7888 57.1 3.9 ak14204954
2016-10-14T11:50:32.110Z 36.4989 -99.0466 5 3.2 us20007e2j
2016-10-14T10:58:12.600Z -37.5421 -73.527 35 4.4 us20007e27
2016-10-14T10:21:08.460Z 37.4513 142.1783 35 4.4 us20007e22
2016-10-14T08:31:21.230Z -19.1325 169.5693 271.95 4.5 us20007e1d
2016-10-14T08:30:38.000Z 57.5189 -155.7755 82.6 2.5 ak14204641
2016-10-14T07:46:57.800Z 18.5802 -64.4445 63 3.2 pr16288004
2016-10-14T06:28:20.890Z 15.2003 147.2835 49.24 4.7 us20007e0x
2016-10-14T04:26:56.020Z 19.3770008 -155.2765045 31.21 2.76 hv61443986
2016-10-14T02:54:51.900Z 19.5685 -65.3981 75 2.6 pr16288003
2016-10-14T01:56:08.300Z 18.12 -64.9405 14 2.5 pr16288001
2016-10-14T01:43:50.000Z 31.0564 50.042 10 4.8 us20007dzl
2016-10-14T01:32:40.910Z -4.173 154.0559 46.2 4.7 us20007dzi
2016-10-14T01:24:26.100Z 18.2752 -66.1652 131 3.9 pr16288000
2016-10-14T00:33:31.000Z 61.8858 -149.1654 19.5 2.6 ak14203471
2016-10-14T00:30:49.800Z -37.2826 178.8081 24.09 4.7 us20007dz4
2016-10-13T23:13:05.740Z -22.7404 172.6608 10 4.7 us20007dyl
2016-10-13T23:07:46.000Z 63.0719 -151.5358 8 3.2 ak14203182
2016-10-13T22:14:24.000Z 58.8229 -153.4964 1.5 2.5 ak14203170
Explanation / Answer
fn=input("Enter a file name : ")
try:
f=open(fn,'r')
for line in f:
wordslist=line.split()
if float(wordslist[4]) > 4.0:
print(line," ", wordslist[4])
except FileNotFoundError:
print(fn, "File Does not exists")
'''
The first line will asks a file name from the user
In the try block, we are opening an accepted file
type filename and it will be stored into variable fn
open fn in read mode
if our typed filename is unable to open, then exception will raised automatically.
exception will prints an error message along with typed file name.
If file is opened successfully, then
the for loop will reterive line by line.
while fetching the data from line by line, inside for loop,
we use a function split, that is associated with the for loop variable line
The split function will seperates and stores the words into a variable called wordslist
i.e wordslist=line.split()
As per our earth_quakes_data concerned, earth quakes information
i.e each and every line was splitted into multiple words based on the default delimiter character a single space.
the splitted words are stored in wordslist
the earth quake information is available at 4th column
so, i compared float(wordslist[4]) > 4.0:,
This condition will prints the number of earthquakes with a magnitude greater than or equal to 4.0.
Excuted Result
==============
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
===== RESTART: F:/personal/PersonalData/Chegg/Oct-2017/13-Oct-2017/eq.py =====
Enter a file name : earth_quakes.dat
earth_quakes.dat File Does not exists
>>>
===== RESTART: F:/personal/PersonalData/Chegg/Oct-2017/13-Oct-2017/eq.py =====
Enter a file name : earth_quakes.data
2016-10-14T18:29:03.610Z 9.1379 125.623 34.97 5.4 us20007e6x
5.4
2016-10-14T18:09:33.930Z -21.8732 -68.4961 118.09 4.7 us20007e6r
4.7
2016-10-14T17:25:24.190Z 16.7085 -60.6958 25.69 4.4 us20007e6i
4.4
2016-10-14T17:23:20.980Z 36.7393 71.162 191.26 4.4 us20007e6h
4.4
2016-10-14T10:58:12.600Z -37.5421 -73.527 35 4.4 us20007e27
4.4
2016-10-14T10:21:08.460Z 37.4513 142.1783 35 4.4 us20007e22
4.4
2016-10-14T08:31:21.230Z -19.1325 169.5693 271.95 4.5 us20007e1d
4.5
2016-10-14T06:28:20.890Z 15.2003 147.2835 49.24 4.7 us20007e0x
4.7
2016-10-14T01:43:50.000Z 31.0564 50.042 10 4.8 us20007dzl
4.8
2016-10-14T01:32:40.910Z -4.173 154.0559 46.2 4.7 us20007dzi
4.7
2016-10-14T00:30:49.800Z -37.2826 178.8081 24.09 4.7 us20007dz4
4.7
2016-10-13T23:13:05.740Z -22.7404 172.6608 10 4.7 us20007dyl
4.7
>>>
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.