使用python3做练习,用eval语句将文本数据转化为列表时,报错SyntaxError: unexpected EOF while parsing 具体代码如下
datals= [ ]
f= open("data.txt",encoding='utf-8')
for line in f :
line = line.replace("\n","")
datals.append(list(map(eval,line.split(","))))
f.close()
修改代码,加入if判断
datals= [ ]
f= open("data.txt",encoding='utf-8')
for line in f :
line = line.replace("\n","")
if line!="": # eval内参数为空时报错,需要加入if判断
datals.append(list(map(eval,line.split(","))))
f.close()
运行后读出数据并没有报错
data.txt如下