【问题标题】:How to make a dictionary from text file using First row as the key如何使用第一行作为键从文本文件制作字典
【发布时间】: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'

【问题讨论】:

  • 作为对问题质量的评论:请尝试始终在问题中包含确切的错误消息,以便我们更轻松地为您提供帮助。

标签: python text key-value csv


【解决方案1】:

如果你想跳过标题,不要在每个循环中调用 next。

with open("covidmonitor.txt", 'r') as f:
    # f.readlines()[1:] read all line except first line
    for cov in f.readlines()[1:]:
        cov = cov.strip()

它似乎是一个 csv 文件。你也可以像这样在python中使用csv包。

import csv

no_cases= []
country= []
cov_dict = {}
with open("covidmonitor.txt", 'r') as f:
    cov = csv.DictReader(f, delimiter=",", skipinitialspace=True)
    for country_data in cov:
        total_count = [float(data) for key, data in c.items() if key != 'COUNTRY']
        no_cases.append(sum(total_count))
        country.append(country_data['COUNTRY'])
        cov_dict[country_data['COUNTRY']] = total_count

【讨论】:

    【解决方案2】:

    从代码来看,no_cases是一个字符串列表,因为confirmactiverecoveredsuspectprobabledeceased都是字符串和

    total_count= (confirm + active + recovered + suspect + probable + deceased)
    

    也是一个字符串,是串联而不是求和。

    在代码最后一行的字符串列表上调用 sum() 应该会产生如下错误:

    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    

    如果您想将它们视为数字,则应将所有这些转换为整数。


    您的代码也存在其他问题。例如,您调用 next(f) 来跳过标题,但实际上是在文件中的行上的 for 循环中执行此操作,因此它可能会跳过每隔一行。

    【讨论】:

    • 对不起,我忘了转换为 int。我会编辑它。谢谢
    【解决方案3】:

    您可以为此使用 Python pandas 包:

    import pandas as pd
    data = pd.read_csv("COVID19.TXT", sep=",")
    covid = data.set_index("COUNTRY").T.to_dict()
    print(covid)
    

    此外,还有一个分析建议,您可以轻松地在 pandas 数据帧上计算各种函数,然后在需要时转换为 dict。在此处查看此链接以获取更多详细信息:Pandas tutorial

    如果您需要更多帮助,请在 cmets 中告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2013-04-01
      • 2018-11-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      相关资源
      最近更新 更多