gpt4 book ai didi

python - 我无法创建一个函数来告诉我 CSV 文件中发生了多少次地震

转载 作者:行者123 更新时间:2023-12-04 15:32:39 25 4
gpt4 key购买 nike

对于我的 CompSci 家庭作业,我得到了一个 CSV 文件,我应该创建代码来打印文件中报告的地震次数。 CSV 的第一行是标题,后面是 13 行数据。因此,我知道有13次地震。我的问题是,在第七行中,我不断收到索引超出范围的错误。

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

这是CSV文件

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


这是我的代码(注意:还没有完成)

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

我的主要问题是为什么会出现此错误以及如何修复它。其次,我将如何在我的 while 循环中实现 if/else 语句。我想说的是,如果该列发生地震,则将其添加到地震总数中,然后移至下一行。如果不是,请在列中添加一个并重试。

最佳答案

让我们试试 DictReader来自 csv 模块:

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

关于python - 我无法创建一个函数来告诉我 CSV 文件中发生了多少次地震,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60851224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com