【发布时间】:2020-06-21 22:05:16
【问题描述】:
我正在尝试将下拉选择列表的列添加到数据框中,然后再将其存储到 FLASK、SQLAlchemy、Python 中的 mysql。你能帮忙吗?得到错误:
TypeError: 'method' object is not subscriptable
以下是用于项目的两个文件。 app.py 和 view_two.html。基本上,我正在开发将所有银行对帐单excel文件提交到mysql的应用程序。首先,我使用 PANDAS 通过将 df 转换为字典来查看数据框。现在在发送到 mysql 之前,我想为每笔交易分配类别,无论是清洁费用、银行费用、食品费用等。
app.py
@app.route('/submit_two', methods=['GET', 'POST'])
def submit_two():
category = (request.form.get['category'])
a_two = session.get('a_two')
df = pd.DataFrame(a_two)
df['Categories'] = category
new_data_two = df.to_dict(orient='record')
a_two = new_data_two
if request.method == 'POST':
# df.fillna(0)
# a_two.fillna(0)
for data in a_two:
fav_two = BankData(process_date=data.get('Process date'), description=data.get(
'Description'), debit=data.get('Debit'), credit=data.get('Credit'), balance=data.get('Balance'), category=data.get('Categories'))
db.session.add(fav_two)
db.session.commit()
print("Bank data submitted")
return render_template("index.html")
view_two.html
<body>
<!-- First Containter Starts -->
<div class="container">
<div class="row">
<div class="col md-12">
<div class="jumbotron p-3">
<!-- table header -->
<!-- Data view Table Starts -->
<form action="{{url_for('submit_two')}}" method="POST">
<table class="table table-hover table-dark">
{% if new_data_two %}
<tr>
{% for key in new_data_two[0] %}
<th> {{ key }} </th>
{% endfor %}
</tr>
{% endif %}
<!-- table rows -->
{% for dict_item in new_data_two %}
<tr>
{% for value in dict_item.values() %}
<td> {{ value }} </td>
{% endfor %}
<td>
<!-- <form action="{{url_for('submit_two')}}" method="POST"> -->
<select id="category" name="category">
<option value="bankFees">Bank Fees</option>
<option value="cleaning">Cleaning</option>
<option value="sundry">Sundry</option>
<option value="telephone">Telephone</option>
</select>
<!-- </form> -->
</td>
</tr>
{% endfor %}
</table>
<button class="btn btn-primary" type="submit" class="btn btn-success float-right btn-lg"
data-toggle="modal" data-target="{{url_for('submit_two')}}">Submit Bank Data</button>
</form>
<!-- Data View Table Ends -->
<!-- <div class="modal-body"> -->
<!-- <div class="jumbotron p-3"> -->
<!-- <form action="{{url_for('submit_two')}}" method="POST"> -->
<!-- <table> -->
<!-- <th> -->
<!-- <label> -->
<!-- <h2>Submit Bank data</h2> -->
<!-- </label> -->
<!-- <input type="file" class="form-control" name="myfile" required="1"> -->
<!-- </th> -->
<!-- <th> -->
<!-- <h2> -->
<!-- <button class="btn btn-primary" type="submit" -->
<!-- class="btn btn-success float-right btn-lg" data-toggle="modal" -->
<!-- data-target="{{url_for('submit_two')}}">Submit Bank Data</button> -->
<!-- </h2> -->
<!-- </th> -->
<!-- </table> -->
<!-- </form> -->
</div>
</div>
</div>
{%endblock%}
【问题讨论】:
标签: python pandas flask sqlalchemy