【问题记录】Python出现NoneType.__format__的问题

问题记录:

调试程序时,发现报错:

 File "C:\Users\HP\PycharmProjects\pythonProject\StudentScoreManagerSystem\stusystem.py", line 297, in <module>
    main()
  File "C:\Users\HP\PycharmProjects\pythonProject\StudentScoreManagerSystem\stusystem.py", line 32, in main
    show()
  File "C:\Users\HP\PycharmProjects\pythonProject\StudentScoreManagerSystem\stusystem.py", line 291, in show
    show_student(student_list)
  File "C:\Users\HP\PycharmProjects\pythonProject\StudentScoreManagerSystem\stusystem.py", line 146, in show_student
    print(format_data.format(item.get('id'),
TypeError: unsupported format string passed to NoneType.__format__

问题描述:

TypeError: unsupported format string passed to NoneType.__format__, 翻译过来是,format格式化输出不支持NoneType类型。

原因分析:

读取文件中学生信息时,对应位置没有内容(获取到的值为 NoneType为空,或者读的值不对应),导致出错。


解决方案:

经过排查发现,是由于自己的粗心大意,在写入文件时为:

student = {'id': id, 'name': name, 'english': english, 'python': python, 'shuxue': shuxue, 'avg': int(avg),'sthhu': stuhu, 'teach': teach, 'zonghe': int(zonghe)}

再看下面的读取文件:

    for item in lst:
        print(format_data.format(item.get('id'),
                                 item.get('name'),
                                 item.get('english'),
                                 item.get('python'),
                                 item.get('shuxue'),
                                 item.get('avg'),
                                 item.get('stuhu'),
                                 item.get('teach'),
                                 item.get('zonghe')
                                 ))

发现问题所在了,读取的是’stuhu’,存入文件的是’sthhu’,赶紧修改,问题解决了。
在这里插入图片描述


版权声明:本文为weixin_50765519原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。