关于中国大学MOOC课程《python语言程序设计》(嵩天)实例11:自动轨迹绘制 报SyntaxError: invalid character in identifier错误的解决方法及原因

首先,该课程实例11的内容为自动绘制轨迹
课程所给定的代码如下

#AutoTraceDraw.py
import turtle as t
t.title('自动轨迹绘制')
t.setup(800,600,0,0)
t.pencolor("red")
t.pensize(5)
#数据读取
datals=[]
f=open("data.txt")
for line in f:
    line=line.replace("\n","")
    datals.append(list(map(eval, line.split(",")))
f.close()
#自动绘制
for i in range(len(datals)):
    t.pencolor(datals[i][3],datals[i][4],datals[i][5])
    t.fd(datals[i][0])
    if datals[i][1]:
        t.right(datals[i][2])
    else:
        t.left(datals[i][2])

data.txt文本内容为

300,0,144,1,0,0
300,0,144,0,1,0
300,0,144,0,0,1
300,0,144,1,1,0
300,0,108,0,1,1
184,0,72,1,0,1
184,0,72,0,0,0
184,0,72,0,0,0
184,0,72,0,0,0
184,1,72,1,0,1
184,1,72,1,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0

代码全部敲进去,首次运行,提示报错

Traceback (most recent call last):
  File "F:/PycharmProjects/untitled/1.py", line 10, in 
    for line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

这里问题主要出在第10行代码

f=open("data.txt")

通过查找资料,原因可能是python识别不了txt文件的编码,这里需要声明一下
可以修改为

f=open("data.txt",encoding="utf-8")

再次运行,依然报错(这个错误也就是本文要说的主要问题,我花了一天的时间查找资料,反复尝试才找到解决办法),错误提示为:

Traceback (most recent call last):
  File "F:/PycharmProjects/untitled/1.py", line 12, in 
    datals.append(list(map(eval, line.split(","))))
  File "", line 1
    300
       ^
SyntaxError: invalid character in identifier

这个问题我一直没有解决办法,于是只有将下面这行语句逐渐拆分出来查找原因

datals.append(list(map(eval, line.split(",")))

初步分析这个问题出在

line.split(",")

对每行的拆分上,于是我用了简易程序来测试,测试程序如下

datals=[]
f=open("data.txt",encoding="utf-8")
for line in f:
    line=line.replace("\n","")
    datals=list(line)
    print(datals)
f.close()

测试结果如下

"F:\Program Files\Python35\python.exe" F:/PycharmProjects/untitled/test1.py
300,0,144,1,0,0
300,0,144,0,1,0
300,0,144,0,0,1
300,0,144,1,1,0
300,0,108,0,1,1
184,0,72,1,0,1
184,0,72,0,0,0
184,0,72,0,0,0
184,0,72,0,0,0
184,1,72,1,0,1
184,1,72,1,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0

Process finished with exit code 0

看似没有问题
然后将上面程序扩展

datals=[]
f=open("data.txt",encoding="utf-8")
for line in f:
    line=line.replace("\n","")
    datals=list(line)
    print(datals)
f.close()

测试列表输出,结果为

"F:\Program Files\Python35\python.exe" F:/PycharmProjects/untitled/test1.py
['\ufeff', '3', '0', '0', ',', '0', ',', '1', '4', '4', ',', '1', ',', '0', ',', '0']
['3', '0', '0', ',', '0', ',', '1', '4', '4', ',', '0', ',', '1', ',', '0']
['3', '0', '0', ',', '0', ',', '1', '4', '4', ',', '0', ',', '0', ',', '1']
['3', '0', '0', ',', '0', ',', '1', '4', '4', ',', '1', ',', '1', ',', '0']
['3', '0', '0', ',', '0', ',', '1', '0', '8', ',', '0', ',', '1', ',', '1']
['1', '8', '4', ',', '0', ',', '7', '2', ',', '1', ',', '0', ',', '1']
['1', '8', '4', ',', '0', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '0', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '0', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '1', ',', '0', ',', '1']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '1', ',', '0', ',', '0']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '0', ',', '0', ',', '0']
['1', '8', '4', ',', '1', ',', '7', '2', ',', '0', ',', '0', ',', '0']

Process finished with exit code 0

很明的可以看出,上面出现了’\ufeff’这样一个不应该出现的代码
看来问题出在这里,于是度娘查找解决办法
可参见https://www.cnblogs.com/chongzi1990/p/8694883.html
简单来说解决办法就是将第11行的代码

f=open("data.txt",encoding="utf-8")

进一步修改为

f=open("data.txt",encoding="utf-8-sig")

关于utf-8与utf-8-sig的简要解释可以参见https://blog.csdn.net/dumeichen/article/details/42124121

至此,该问题圆满解决,终于见到如课程所要展示的图案了

你可能感兴趣的:(python问题解决办法)