您好,我是Web App Dev的新手。我正在尝试制作一个操纵CSV文件的应用程序。
让我在下面陈述问题之前粘贴我的代码:
#!/usr/bin/env python
import os
import pandas as pd
from flask import Flask, request,render_template, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
# create app
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/home/Firiyuu77/mysite/uploads'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','csv','xlsx'])
def allowed_file(filename):
return '.' in filename and
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def main():
return render_template('index.html')
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
return redirect(url_for('uploaded_file',
filename=filename))
# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("80"),
debug=True
)
#--------------------------------------------------------
#--------------------------------------------------------
#--------------------------------------------------------UNFINISHED PART
def forecastvalues():
fileName = "test.csv"
records = pd.read_csv(fileName, header=None, nrows=5)
for i in records:
rem = records.iloc([i], [0])
sold1 = records.iloc([i], [1])
sold2 = records.iloc([i], [2])
rem = int(rem)
sold1 = int(sold1)
sold2 = int(sold2)
result = forecast(rem,sold1,sold2)
records.set_value([i], [4], result)
pd.to_csv('test.csv')
#--------------------------------------------------------
#
#
#
#
# ------------------------------------------------------------
# MAIN Program
# ------------------------------------------------------------
#------------------------------------------------------------------------------------------------
def calculate(r,t,l):
return ((l+t)/2)*3
def forecast(rem, sold1, sold2):
if (rem == 0 and sold1 == 0 and sold2 ==0): #All ZERO
return 15
elif (rem == 0 and sold1 == 0 and sold2 < 10): #ALL FOR ONE PRODUCT VALUE
return sold2*3
elif (rem == 0 and sold1 < 10 and sold2 ==0):
return sold1*3
elif (rem < 10 and sold1 == 0 and sold2 == 0):
return rem*3
#END FOR ONE PRODUCT VALUE
elif (rem>= 10 and sold1>=10 and sold2>=10):
if((rem/3)>=(sold1+10) or (rem/3)>=(sold1+10)):
return 0
else:
return calculate(rem,sold1,sold2)-rem
elif (rem<10 and sold1<10 and sold2<10):
return calculate(rem,sold1,sold2)
elif (rem == 0 and sold1>=10 and sold2>=10):
return calculate(rem,sold1,sold2)
else:
return sold1
@app.route('/forecaster', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
# show html form
return '''
<form method="post">
<h3>Type in the remaining stocks: </h3> <input type="text" name="remaining" />
<br/>
<h3>Type in the stocks for the past month: </h3> <input type="text" name="sold1" />
<br/>
<h3>Type in the stocks for the the month before the past month: </h3> <input type="text" name="sold2" />
<br/>
<br/>
<input type="submit" value="forecast" />
</form>
'''
elif request.method == 'POST':
# calculate result
rem = int(request.form.get('remaining'))
sold1 = int(request.form.get('sold1'))
sold2 = int(request.form.get('sold2'))
result = forecast(rem,sold1,sold2)
return '<h1>Result: %s</h1>' % result
在forecastValues((我想评估CSV每一行的值,并使用预测((评估每个值,并将评估结果放在每一行的第四列中。
所以我在那里循环。并将这些值转化为整数,将它们分配到变量REM,已销售1和2个,然后将它们插入forecast(rem, sold1, sold2)
中。然后,我将预测的返回值分配为结果,然后将其放入循环中,以便将其分配给行的第四列。我认为输出是这样的:
来自这些输入
1 2 1
1 3 1
1 2 2
使用文件完成程序
完成此输出1 2 1 result
1 3 1 result
1 2 2 result
但似乎对CSV文件没有影响吗?我将测试CSV的名称写为文件名,以便可以测试。我的熊猫功能中有什么问题吗?还是我以错误的方式实施了?我在制作代码时使用了骗子的备忘录。
您正在调用pd.to_csv('test.csv')
,但我不认为这会因为没有pandas.to_csv
方法而起作用。但是,有一种pandas.DataFrame.to_csv
方法,它将执行您要完成的工作。您需要致电
records.to_csv('test.csv')