gpt4 book ai didi

python - 写入文件的条目不正确

转载 作者:太空宇宙 更新时间:2023-11-03 15:29:56 25 4
gpt4 key购买 nike

我正在编写一个 Flask 应用程序,供某人管理我的数据集。它应该在文本区域中显示一条推文,用户选择单选按钮上的选项并提交,然后将其写入文件中。然而,问题是文本区域中显示的推文与写入文件中的推文不同。如果推文 A 显示在文本区域中,则下一条推文(推文 B)将写入文件,即使该人对推文 A 进行了评分。出了什么问题?我的html模板和flask代码如下:

flask :

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>

最佳答案

您可以使用文本区域中的值来更新数据文件。请参阅下面的测试代码:

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>

关于python - 写入文件的条目不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com