我正在为一个策划我的数据集的人写一个烧瓶应用程序。它应该在textarea中显示一条推文,然后该人在单选按钮上选择选项并提交它,并将其写入文件。但问题是,textarea中的推文显示与写入文件的信息不同。如果推文A显示在文本区域中,则下一条推文(推文B)会写入文件,即使该人评价了推文A.出了什么问题?我的html模板和烧瓶代码如下:
烧瓶中:
from flask import Flask, render_template, request
import csv
app = Flask(__name__)
with open('sqldata.csv', 'r') as f:
reader = csv.reader(f)
data = [row for row in reader]
x = []
@app.route('/', methods=['GET','POST'])
def index():
datapoint = data[len(x)+1][2]
db={'Entry':datapoint}
if request.method == 'POST':
print request.form['Button1']
but1 = request.form['Button1']
but2 = request.form['Button2']
print sent
but3 = request.form['Button3']
with open('path/%s.csv' % but2, 'a') as f:
writer = csv.writer(f)
writer.writerows([(datapoint, but1)])
with open('path/data.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([but3])
x.append(len(x))
return render_template('index.html', db=db)
if __name__ == '__main__':
app.run(host, port,debug=True)
HTML模板:
<html>
<head> Sentiment Analysis Dataset</head>
<style>
.scrollabletextbox {
height:50px;
width:200px;
font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;
font-size: 82%;
overflow:scroll;
}
</style>
<form method='POST'>
<b> Unclassified Text </b>
<textarea class='scrollabletextbox' name='Text' readonly>
{{db.Entry}}
</textarea>
<br>
<b> But2 </b> <br>
Positive:<input type='radio' name='Button2' value='1'><br>
Negative:<input type='radio' name = 'Button2' value='2'><br>
Noise:<input type='radio' name = 'Button2' value = '3'><br>
1<input type='radio' name = 'Button1' value='1'>
<input type='radio' name = 'Button1' value='2'>
<input type='radio' name = 'Button1' value='3'>
<input type='radio' name = 'Button1' value='4'>
<input type='radio' name = 'Button1' value='5'>
<input type='radio' name = 'Button1' value='6'>
<input type='radio' name = 'Button1' value='7'>
<input type='radio' name = 'Button1' value='8'>
<input type='radio' name = 'Button1' value='9'>
<input type='radio' name = 'Button1' value='10'>10<br>
<b>But3</b><br>
<input type='text' name='Button3'><br>
<input type='submit' value='Submit' name='Submit'>
</form>
</html>
答案 0 :(得分:1)
您可以使用textarea中的值来更新数据文件。请参阅以下测试代码:
from flask import Flask, render_template, request
import csv, os
import jinja2
app = Flask(__name__)
with open('sqldata.csv','r') as f:
reader = csv.reader(f)
data = [row for row in reader]
x = []
@app.route('/', methods=['GET','POST'])
def index():
datapoint = data[len(x)][2]
db={'Entry':datapoint}
if request.method == 'POST':
but1 = request.form['Button1']
but2 = request.form['Button2']
but3 = request.form['Button3']
with open('%s.csv' % but2,'a') as f:
writer = csv.writer(f)
writer.writerows([(request.form['Text'], but1)])
with open('data.csv','a') as f:
writer = csv.writer(f)
writer.writerow([but3])
x.append(len(x))
return render_template('index.html',db=db)
if __name__ == '__main__':
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates'))
app.run(debug=True)
模板代码:
<html>
<head> Sentiment Analysis Dataset</head>
<style>
.scrollabletextbox {
height:50px;
width:200px;
font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;
font-size: 82%;
overflow:scroll;
}
</style>
<form method='POST'>
<b> Unclassified Text</b>
<textarea class='scrollabletextbox' name='Text'>{{db.Entry}}</textarea>
<br>
<b> But2 </b> <br>
Positive:<input type='radio' name='Button2' value='1'><br>
Negative:<input type='radio' name = 'Button2' value='2'><br>
Noise:<input type='radio' name = 'Button2' value = '3'><br>
1<input type='radio' name = 'Button1' value='1'>
<input type='radio' name = 'Button1' value='2'>
<input type='radio' name = 'Button1' value='3'>
<input type='radio' name = 'Button1' value='4'>
<input type='radio' name = 'Button1' value='5'>
<input type='radio' name = 'Button1' value='6'>
<input type='radio' name = 'Button1' value='7'>
<input type='radio' name = 'Button1' value='8'>
<input type='radio' name = 'Button1' value='9'>
<input type='radio' name = 'Button1' value='10'>10<br>
<b>But3</b><br>
<input type='text' name='Button3'><br>
<input type='submit' value='Submit' name='Submit'>
</form>
</html>