提问:python运行出现SyntaxError: invalid syntax错误怎么解决?

问题相关代码,请勿粘贴截图

def toolGetWord(inputstring, index, d):

if inputstring[0] != d:
    inputstring = str(d) + inputstring

if inputstring[-1] != d:
    inputstring = inputstring + str(d)

p_d = {}
c = 0
for i in range(len(inputstring)):
    if inputstring[i] == d and inputstring[i+1] != d:
        p_d[c] = i
        c = c + 1

return inputstring[p_d[index]:p_d[index + 1]]

with open(pathMS1, 'rb') as f:

for line in f.readlines():
    str_line = line.decode('UTF-8')
    str_line = eval(str_line)      #line85
    len_line = len(str_line)
    if len_line > 1:
        st = toolGetWord(str_line, 0, ' ')
        if st == "S":

            tmpScan = int(toolGetWord(str_line, 1, ' '))
            Scan1_A.append(tmpScan)
        else:
            pass
运行结果及报错内容

Traceback (most recent call last):
File "F:/学习资料/学习/大三/生物医学信息学/作业/11.23.py", line 85, in
str_line = eval(str_line)
File "", line 1
H CreationDate Fri Nov 26 22:17:39 2021
^
SyntaxError: invalid syntax

readlines读取的字符串结尾会带有'\n',所以在eval前需要处理,在有eval是一个特别容易出错且不容易调试的函数,不建议使用,使用时建议加上报错拦截
代码修改如下 第85行,建议加上try except ,如不需要刻意删除,只保留 str_line = eval(str_line.replace('\n', ' ')) # line85

        try:
            str_line = eval(str_line.replace('\n', ' '))  # line85

        except Exception as e:
            print("出错啦", f"错误数据是{e}")