打开(url_for('静态',filename =' bmidata.txt')为f:
上述行导致语法错误(标记为' as')。该代码是以下内容的一部分:
@app.route('/display')
def display():
page_info = {
'title':'Assignment Flask',
'heading': 'Python Flask Assignment'
}
filedata = []
with open(url_for('static', filename='bmidata.txt') as f:
for line in f:
str = line
dataarray = str.split(',')
it =iter(dataarray)
name = it.next()
height = it.next()
weight = it.next()
newPerson = Person(name, height,weight)
filedata.append(newPerson)
return render_template('display.html', info = page_info, fileinfo = filedata)
任何帮助表示赞赏
答案 0 :(得分:6)
在这一行:
with open(url_for('static', filename='bmidata.txt') as f:
你错过了一个结束括号:
with open(url_for('static', filename='bmidata.txt')) as f:
这就是SyntaxError的原因。
打开文件无法正常工作,因为open
不接受网址。如果需要打开静态文件,请使用with app.open_resource('static/bmidata.txt') as f:
或在文件系统中查找文件的路径。