【发布时间】:2012-12-27 07:30:18
【问题描述】:
环境: Windows 7的 Python2.7 Eclipse SDK 3.7.2
尝试学习http://www.youtube.com/watch?v=Z-HpXbhVuGo 的教程 我收到一条错误消息:
============================== 错误================= ============= 回溯(最近一次通话最后): get_module_from_str 中的文件“C:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydev_runfiles.py”,第 432 行 mod = __import(模组名称) 文件“C:\Users\lenovo\workspace\assignment3\fileiotest.py”,第 17 行 print(bestStudent[i] + ‘ 得了分’ + i) ^ SyntaxError:无效的语法 错误:模块:fileiotest 无法导入(文件:C:/Users/lenovo/workspace/assignment3/fileiotest.py)。
.................................................. ......................................
#-*- coding: utf8 -*-
from __future__ import print_function
bestStudent = {}
f = open ("C:/Users/lenovo/workspace/assignment3/studentgrades.txt")
for line in f:
name, grade = line.split()
bestStudent[grade] = name
f.close()
bestStudentStr= ""
for i in sorted(bestStudent.keys(), reverse=True):
print bestStudent[i] + 'scored a ' + i
bestStudentStr += bestStudent[i] + ‘ scored a ‘ + i + ‘\n’
bestStudentStr = ‘\nThe Best Students Ranked\n\n’ + bestStudentStr
print(bestStudentStr)
outToFile = open(‘studentrank.txt’, mode=’w', encoding='utf-8′)
outToFile.write(bestStudentStr)
outToFile.close()
print(‘Finished update’)
【问题讨论】:
-
SyntaxError:invalid syntax 突出显示以下代码行:(bestStudent[i]
-
您有一些非 ASCII 引号
’与 ASCII 引号'混合在一起。把所有的非 ASCII 改成 ASCII 就可以了。 -
我还在那里看到了一个反引号
`。您还需要将其更改为 ASCII 引号。