繁体   English   中英

Python中的TypeError:不支持的格式字符串传递给dict .__ format__

[英]TypeError in Python: unsupported format string passed to 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]))

不确定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即可将员工姓名映射到员工dict。

这是我想出的解决方案:

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]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM