【发布时间】:2020-05-16 13:29:33
【问题描述】:
我正在尝试从文本文件到字典进行 COVID 监控,以检查每个国家/地区的病例。 我想制作这种格式的字典。
covid ={country{confirmed:value, active:value, recovered:value, suspect:value, probable:value,
deceased:value}
基于此文本文件。
COUNTRY, CONFIRMED, ACTIVE, RECOVERED, SUSPECT, PROBABLE, DECEASED
COUNTRY-A,3,4,2,1,0,0
COUNTRY-B,1,2,0,2,0,0
COUNTRY-C,4,2,0,0,3,0
COUNTRY-D,1,1,1,3,0,0
COUNTRY-E,3,2,0,2,0,0
COUNTRY-F,2,0,1,2,0,0
COUNTRY-G,0,0,1,4,0,0
我试过这段代码,但它会打印出 COUNTRY、CONFIRMED、ACTIVE、RECOVERED、SUSPECT、PROBABLE、DECEASED 每次计算每个国家/地区的病例总数时都会出现错误。
我试过这段代码:
def covid_monitoring():
country = []
cov_dict = {}
no_cases = []
with open("covidmonitor.txt", 'r') as f:
for cov in f:
cov = cov.strip()
next(f) # skip header
if len(cov) >= 1:
cov_line = cov.split(",")
country.append(cov_line[0].strip())
confirmed_file = cov_line[0].strip()
active_file = cov_line[1].strip()
recovered_file = cov_line[2].rstrip()
suspected_file = cov_line[3].strip()
probable_file = cov_line[4].strip()
probable_file = cov_line[5].strip()
deceased_file = cov_line[6].strip(';')
if confirmed_file not in cov_dict:
cov_dict[confirmed_file] = [(active_file, recovered_file, suspected_file, probable_file, probable_file, deceased_file)]
else:
cov_dict[confirmed_file].append((active_file, recovered_file, suspected_file, probable_file, probable_file, deceased_file))
# print(cov_dict)
for cntry in country:
if cntry in cov_dict:
for confirm, active, recovered, suspect, probable, deceased in cov_dict[cntry]:
print("\tCOUNTRY:{cntry}")
print("\tCONFIRMED:{confirm} ")
print("\tACTIVE:{active} ")
print("\tRECOVERED:{recovered} ")
print("\tSUSPECTED:{suspect} ")
print("\tPROPBABLE:{probable} ")
print("\tDECEASED:{deceased} ")
total_count = int(confirm) + int(active) + int(recovered) + int(suspect) + int(probable) + int(deceased)
no_cases.apped(total_count)
print(sum(no_cases)
这是我的错误:
total_count = int(confirm) + int(active) + int(recovered) + int(suspect) + int(probable) + int(deceased)
ValueError: invalid literal for int() with base 10: 'CONFIRMED'
【问题讨论】:
-
作为对问题质量的评论:请尝试始终在问题中包含确切的错误消息,以便我们更轻松地为您提供帮助。