我有一个字典(有多个对象)我正在尝试创建一个列表,该列表汇总了每个对象的一些值。到目前为止,我有:
import csv,os,re
#numpy.corrcoef(list1, list2)[0, 1]
input_dict = csv.DictReader(open("./MCPlayerData/AllPlayerData2.csv"))
npi_scores=[]
for person in input_dict:
#print person
i=0
for key in person:
if re.match(r'npi[0-9]+', key):
#print key,'=',person[key] #returns npi0=1,npi1=3,npi3=2,etc
try:
i+=person[key]
#print(person[key])
except TypeError:
i="NA" #returns NA because one of the values wasnt filled out with an integer
break
npi_scores.append(i)
break
print npi_scores #returns sum of npi scores for one person
print('DONE')
当我运行此代码时,我会根据第一个元素得到NA
。如果值不是整数,那么我期望的是,但所有都是整数。有什么想法吗?