I'm having trouble making a function to tell me how many earthquakes occur in a CSV file

Viewed 86

For my CompSci homework, I was given a CSV file and am supposed to create code to print the number of earthquakes reported in the file. The first line of the CSV is a header, followed by 13 lines of data. Therefore, I know there are 13 earthquakes. My trouble is that in line seven I keep getting an index out of range error.

Traceback (most recent call last):
  File "D:\Thonny\Files\march25.py", line 7, in <module>
    while lineBeingRead[disasterType] != "earthquake":
IndexError: list index out of range

Here is the CSV file

time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type,horizontalError,depthError,magError,magNst,status,locationSource,magSource
2020-03-23T01:17:15.310Z,40.779,-112.053,9.98,3.94,ml,44,52,0.01894,0.19,uu,uu60369062,2020-03-24T22:26:20.015Z,"8km NNE of Magna, Utah",earthquake,0.28,0.44,0.205,20,reviewed,uu,uu
2020-03-22T05:24:03.828Z,45.8972,15.9662,10,5.4,mww,,13,0.802,0.8,us,us70008dx7,2020-03-24T23:09:37.745Z,"3km SW of Kasina, Croatia",earthquake,4.6,1.8,0.052,35,reviewed,us,us
2020-03-21T01:33:35.369Z,39.1112,-119.7361,8.4,4.5,ml,31,28.07,0.029,0.1334,nn,nn00719663,2020-03-24T21:50:29.491Z,"6km N of Johnson Lane, Nevada",earthquake,,1.2,0.38,5,reviewed,nn,nn
2020-03-20T01:33:15.377Z,28.6065,87.3298,10,5.7,mww,,30,0.79,0.89,us,us70008cld,2020-03-24T16:06:24.685Z,"24km E of Xegar, China",earthquake,6.5,1.7,0.063,24,reviewed,us,us
2020-03-18T22:08:20.920Z,40.3475,-124.4561667,28.61,5.21,mw,34,248,0.105,0.16,nc,nc73355700,2020-03-24T22:18:27.177Z,"15km W of Petrolia, CA",earthquake,0.78,0.55,,5,reviewed,nc,nc
2020-03-18T19:12:23.610Z,40.751,-112.059,10.65,4.57,mw,39,47,0.02976,0.21,uu,uu60364832,2020-03-24T22:25:19.766Z,"5km NE of Magna, Utah",earthquake,0.32,0.53,,12,reviewed,uu,uu
2020-03-18T17:45:38.838Z,-11.0591,115.131,17.59,6.2,mww,,24,2.747,1.06,us,us60008hzl,2020-03-21T16:28:15.002Z,"246km S of Kangin, Indonesia",earthquake,7.2,3,0.038,65,reviewed,us,us
2020-03-18T14:02:12.830Z,40.7605,-112.0693333,9.07,4.59,mw,21,58,0.03587,0.19,uu,uu60363822,2020-03-24T22:24:20.649Z,"7km NNE of Magna, Utah",earthquake,0.39,0.8,,17,reviewed,uu,uu
2020-03-18T13:09:31.530Z,40.751,-112.0783333,11.9,5.7,mw,54,50,0.04022,0.2,uu,uu60363602,2020-03-25T00:28:33.745Z,"6km NNE of Magna, Utah",earthquake,0.3,0.39,,0,reviewed,uu,uu
2020-03-14T10:01:17.383Z,-27.4196,-175.6847,10,6.3,mww,,22,2.687,1.03,us,us60008fl8,2020-03-15T10:03:53.243Z,"298km NE of Raoul Island, New Zealand",earthquake,7.6,1.8,0.063,24,reviewed,us,us
2020-03-11T01:17:16.090Z,39.4751667,-120.2455,7.87,3.71,mw,16,70,0.1363,0.07,nc,nc73352615,2020-03-21T15:21:43.839Z,"17km NNW of Truckee, CA",earthquake,0.28,0.91,,4,reviewed,nc,nc
2020-03-09T02:59:08.860Z,40.3916667,-125.0936667,3.19,5.77,mw,40,277,0.5832,0.29,nc,nc73351710,2020-03-22T20:17:55.040Z,"69km W of Petrolia, CA",earthquake,0.99,1.38,,4,reviewed,nc,nc
2020-03-07T03:52:03.180Z,31.6893333,-114.5405,9.98,5.49,mw,13,106,0.3208,0.35,ci,ci38385946,2020-03-22T03:11:14.628Z,"71km SE of Estacion Coahuila, B.C., MX",earthquake,0.78,31.61,,6,reviewed,ci,ci


Here's my code(note: not complete yet)

import csv
with open("earthquakes.csv", "r") as inFile:
    csvReader = csv.reader(inFile)
    lineBeingRead = next(csvReader)
    earthquakes = 0 #will be used for the final print value
    column = 0 #each line has a value called type, which is referred to as earthquake
    while lineBeingRead[column] != "earthquake": 
        column = column + 1
        if column == "earthquake":
            lineBeingRead = next(csvReader)
            earthquakes = earthquakes
        #else
        #print

My main question is why am I getting this error and what can I do to fix it. Secondly, how would I go about implementing an if / else statement in my while loop. I want to say if that column has an earthquake, add it to the total number of earthquakes and move onto the next line. if not, add one to column and try again.

2 Answers

Let us try with DictReader from csv module:

earthquakes = 0
with open('earthquakes.csv', 'r') as file:
    reader = csv.DictReader(file)
    for dct in reader:
        if dct['type'].lower() == 'earthquake':
            earthquakes += 1

>>> earthquakes

13
print(sum("earthquake" in line
      for line in open("earthquakes.csv", "r"))

This will open the file, iterate through the lines, and return True or False according to whether "earthquake" is in that line. summing that series treats True as 1, and False as 0. In short, a count of how many lines contain "earthquake".

Related