python/flask在flas中打开文本文件

2024-10-01 19:20:24 发布

您现在位置:Python中文网/ 问答频道 /正文

打开(url_for('static',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)

有什么帮助吗


Tags: infotxturlflaskforasdisplaypage
1条回答
网友
1楼 · 发布于 2024-10-01 19:20:24

在这一行中:

with open(url_for('static', filename='bmidata.txt') as f:

您缺少一个右括号:

^{pr2}$

这就是SyntaxError的原因。在

以这种方式打开文件不起作用,因为open不接受URL。如果需要打开静态文件,请使用with app.open_resource('static/bmidata.txt') as f:或在文件系统上找到该文件的路径。在

相关问题 更多 >

    热门问题