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

标签 python html flask

我正在编写一个 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/

相关文章:

python - 文件处理,写作,阅读

html - 导航栏 |显示 flex |漂浮?

html - 禁用 <li> 内的链接 <a>

python - 在 flask 的 jsonify() 中缩小 JSON

flask - jinja2.exceptions.TemplateNotFound : bootstrap/base.html

Python:如何在 Flask 应用程序的表格中显示来自 MySQL 查询的数据

Python:是否可以在一行中使用 if 语句判断输入

python绘图日期轴作为字符串而不是日期

python - 在python中打印出$和*+2字符之间的所有字符串

javascript - 我需要在提交时的同一页面上打印以 HTML 表单输入的值