Python csvdictreader()的键错误为';当函数列标题的参数为
这是我的代码,我一直收到一个关键错误。我已经用我的初始化器代码为我的类打开了这个文件,但问题是我一直得到这个文件的KeyError。我知道当我在函数内打开文件时它可以工作,但是当我在函数外打开它时它不工作,我得到一个keyrerror。如果你需要完整的代码,请告诉我。除了导入csv,我不能使用pandas或任何其他模块。我知道这样做会更容易:) 完整代码:Python csvdictreader()的键错误为';当函数列标题的参数为,python,keyerror,Python,Keyerror,这是我的代码,我一直收到一个关键错误。我已经用我的初始化器代码为我的类打开了这个文件,但问题是我一直得到这个文件的KeyError。我知道当我在函数内打开文件时它可以工作,但是当我在函数外打开它时它不工作,我得到一个keyrerror。如果你需要完整的代码,请告诉我。除了导入csv,我不能使用pandas或任何其他模块。我知道这样做会更容易:) 完整代码: class HWReader(): def __init__(self, load_data): '''i
class HWReader():
def __init__(self, load_data):
'''initializer that sets filename as a parameter that can be used within all functions'''
self.load_data = open(load_data, "r")
def get_saps(self, record_name):
'''finds the saps score for a record name'''
for row in csv.DictReader(self.load_data, delimiter = ','):
if row['RecordID'] == record_name:
return (row['SAPS-I'])
def calc_avg(self, specific, filter, logic, threshold):
'''calculates avg for the column determine by specific based off the filter parameters given by
filter, logic, threshold'''
if isinstance(threshold, str):
threshold = float(threshold)
avg_file = csv.DictReader(self.load_data)
running_sum = 0
running_count = 0
for row in avg_file:
value = int(row[filter])
if logic == 'lt' and value < threshold:
include = True
elif logic == 'gt' and value > threshold:
include = True
elif logic == 'lte' and value <= threshold:
include = True
elif logic == 'gte' and value >= threshold:
include = True
else:
include = False
if include:
running_sum += int(row[specific])
running_count += 1
return (round(running_sum / running_count,2))
class HWReader():
定义初始化(自,加载数据):
''将文件名设置为可在所有函数中使用的参数的初始值设定项''
self.load\u data=open(load\u data,“r”)
def get_saps(自我,记录名称):
''查找记录名''的saps分数'
对于csv.DictReader中的行(self.load_数据,分隔符=','):
如果行['RecordID']==记录名称:
返回(第['SAPS-I'行])
def calc_平均值(自身、特定、过滤器、逻辑、阈值):
''根据以下公式给出的过滤器参数计算由特定值确定的列的平均值:
筛选器、逻辑、阈值“”
如果存在(阈值,str):
阈值=浮动(阈值)
avg_file=csv.DictReader(self.load_数据)
正在运行的\u sum=0
正在运行\u计数=0
对于avg_文件中的行:
value=int(行[过滤器])
如果逻辑=='lt'且值<阈值:
include=True
elif逻辑=='gt'和值>阈值:
include=True
elif逻辑=='lte'和值=阈值:
include=True
其他:
include=False
如果包括:
运行_sum+=int(行[特定])
运行计数+=1
返回(四舍五入(运行求和/运行计数,2))
self.load\u data的定义在哪里?我继续添加了完整的代码,self.load\u data是self.load\u data=open(load\u data,“r”)
对于我来说,代码工作得很好,没有任何错误。真的吗?这太奇怪了,它一直给我那个代码错误,我不明白为什么会发生。
value = int(row[filter])
KeyError: 'SOFA'
Sample Data:
RecordID SAPS-I SOFA Length_of_stay
132539 6 1 5
132540 16 8 8
132541 21 11 19
132545 17 2 4
132547 14 11 6
132548 14 4 9
132551 19 8 6
132554 11 0 17
class HWReader():
def __init__(self, load_data):
'''initializer that sets filename as a parameter that can be used within all functions'''
self.load_data = open(load_data, "r")
def get_saps(self, record_name):
'''finds the saps score for a record name'''
for row in csv.DictReader(self.load_data, delimiter = ','):
if row['RecordID'] == record_name:
return (row['SAPS-I'])
def calc_avg(self, specific, filter, logic, threshold):
'''calculates avg for the column determine by specific based off the filter parameters given by
filter, logic, threshold'''
if isinstance(threshold, str):
threshold = float(threshold)
avg_file = csv.DictReader(self.load_data)
running_sum = 0
running_count = 0
for row in avg_file:
value = int(row[filter])
if logic == 'lt' and value < threshold:
include = True
elif logic == 'gt' and value > threshold:
include = True
elif logic == 'lte' and value <= threshold:
include = True
elif logic == 'gte' and value >= threshold:
include = True
else:
include = False
if include:
running_sum += int(row[specific])
running_count += 1
return (round(running_sum / running_count,2))