小白问,python中SyntaxError怎么解决?

应该是这个代码出错了,但我怎么都检查不出来:
def show_student(lst):
    if len(lst) == 0:
        print('没有查询到学生信息,无数据显示!!!')
        return
    # 定义标题的显示格式
    format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
    print(format_title.format('ID', '姓名', '英语成绩', 'python成绩', 'java成绩', '总成绩'))
    # 定义内容的显示格式
    format_date = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}'
    for item in lst:
        print(format_date.format(item.get('id'),
                                 item.get('name'),
                                 item.get('english'),
                                 item.get('python'),
                                 item.get('java'),
                                 int(item.get('english'))+int(item.get('python'))+int(item.get('java'))
                                 ))

 

Traceback (most recent call last):
  File "D:\python practice\studentsys\stusystem.py", line 247, in <module>
    main()
  File "D:\python practice\studentsys\stusystem.py", line 23, in main
    search()
  File "D:\python practice\studentsys\stusystem.py", line 109, in search
    d = dict(eval(item))
  File "<string>", line 1
    
    ^
SyntaxError: unexpected EOF while parsing

Process finished with exit code 1
 

你想要的是这样的效果吗?

 

你传入的 lst 有吗?可以发一份出来

 

我模拟了下你的数据, 应该是这样的了

 

错误在109行,item不是字典格式

你的 dict( ) 的参数满足 :列表格式: dict([("id",1),("name","xxxx")]), 元组形式 : dict((("id",1),("name","xxxx"))), 也可以是关键字形式 : dict("id"=2,"name"="xxxx")就可以了

正如给你骨质唱疏松所解答的那样,如果lst 是字典列表的话,你的代码片断没有错,从错误信息看,是你的其他代码有错,在d = dict(eval(item))这一行,这里item必须是字典形式字符串,如果item本身就是字典的话,就要改为if d===item['id']或d==item['name']建立搜索字段。

另外你的代码还有语法错误,检查一下各行有没有写完整,有没有漏掉“)”等。

我也是小白,不过我想请教一下你的这段代码是啥意思??

format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'

看不懂唉!!