环境: Windows 7的 Python2.7 Eclipse SDK 3.7.2
尝试按照http://www.youtube.com/watch?v=Z-HpXbhVuGo中的教程进行操作 我收到一条错误消息:
=============================错误================= ============ Traceback(最近一次调用最后一次): 文件“C:\ eclipse \ plugins \ org.python.pydev_2.7.1.2012100913 \ pysrc \ pydev_runfiles.py”,第432行, get_module_from_str mod = __import (modname) 文件“C:\ Users \ lenovo \ workspace \ assignment3 \ fileiotest.py”,第17行 打印(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’)
答案 0 :(得分:0)
突出显示语法错误bestStudent[i]
用
print (bestStudent[i] + 'scored a ' + i)
此外,您的代码中包含大量'而非'单个qoutes,您可以直接替换
解决这些问题,我认为它应该有效。
答案 1 :(得分:0)
替换了非ascii字符:
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")
当您从pdf,doc etc文件复制源时会发生这种情况。最好的解决方案是在没有unicode支持的文本编辑器中进行复制/粘贴,然后你会看到?
标记有什么错误。