首页 > 解决方案 > Python 中的 TypeError:传递给 dict.__format__ 的格式字符串不受支持

问题描述

作为 python 的新手,我正在编写一个代码,我需要根据用户的输入显示列表的结果。

例如,我有一个员工列表,我想显示用户输入其姓名的员工的结果。

但是,当我尝试执行相同操作时,会出现以下错误:

TypeError:unsupported format string passed to dict.__format__

我的代码如下所示:

emp_dict = dict()
emp_list = list()
with open('m04_lab_profiles','r') as people:
    for p in people:
        tmp = p.strip().split(',')
        emp_info = dict()
        emp_info['Name'] = tmp[0]
        emp_info['Location'] = tmp[1]
        emp_info['Status'] = tmp[2]
        emp_info['Employer'] = tmp[3]
        emp_info['Job'] = tmp[4]
        emp_dict[tmp[0]] = emp_list
        emp_list.append(emp_info)

    for info in emp_list:
              print("{0:20}   {1:25}  {2:20}  {3:20}  {4:45}".format(info['Name'],info['Location'],info['Status'],info['Employer'],info['Job']))

 while True:
    name = input("Enter the name of employee: ")
    if len(name) == 0:
        break
    if name not in emp_dict:
        print("{} not found!".format(name))
        continue
    tmp = emp_dict[name]
    print("{0:20}   {1:25}  {2:20}  {3:45}  {4:45}".format(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]))

标签: python

解决方案


不是 100% 确定,但我认为错误的根源在于:

emp_dict[tmp[0]] = emp_list

您将emp_dict 中的员工姓名映射到在当前构建emp_dict 阶段添加的员工列表。

因此,当您到达此处时 tmp = emp_dict[name]- 您尝试使用员工字典 (tmp) 列表格式化字符串,但这是行不通的。

为了解决这个问题,我建议去掉 emp_list,因为它有点多余,而只是有一个名为的字典emp_dict,它将员工姓名映射到员工字典。然后,您可以使用 获取员工姓名列表emp_dict.keys()

然后,在使用文件中的数据创建 emp_dict 时,只需调用emp_dict[tmp[0]] = emp_info而不是emp_dict[tmp[0]] = emp_list将员工姓名映射到员工字典。

这是我想出的一个解决方案:

emp_dict = dict()
# emp_list = list() - Removed

with open('m04_lab_profiles','r') as people:
    for p in people:
        tmp = p.strip().split(',')
        emp_info = dict()
        emp_info['Name'] = tmp[0]
        emp_info['Location'] = tmp[1]
        emp_info['Status'] = tmp[2]
        emp_info['Employer'] = tmp[3]
        emp_info['Job'] = tmp[4]

        emp_dict[tmp[0]] = emp_info # Changed emp_list to emp_info

        # emp_list.append(emp_info) - Removed

    for emp_name in emp_dict.keys(): # For each employee name in the emp_dict dictionary
              print("{0:20}   {1:25}  {2:20}  {3:20}  {4:45}".format(emp_dict[emp_name]['Name'],
                                                                     emp_dict[emp_name]['Location'],
                                                                     emp_dict[emp_name]['Status'],
                                                                     emp_dict[emp_name]['Employer'],
                                                                     emp_dict[emp_name]['Job']))

while True:
    name = input("Enter the name of employee: ")
    if len(name) == 0:
        break

    if name not in emp_dict.keys(): # Added .keys() to get list of names
        print("{} not found!".format(name))
        continue

    tmp = emp_dict[name] # Now tmp is actually an emp_info dict not an emp_list
    print("{0:20}   {1:25}  {2:20}  {3:45}  {4:45}".format(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]))

推荐阅读