csv - Python Evaluate Values on Sheet Using Pandas Read then Write on the File -
hello i'm new web app dev. i'm trying make app, manipulates csv file.
let me paste code before state problem below:
#!/usr/bin/env python import os import pandas pd flask import flask, request,render_template, redirect, url_for, send_from_directory 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 , \ filename.rsplit('.', 1)[1] in app.config['allowed_extensions'] @app.route('/') def main(): return render_template('index.html') # route process file upload @app.route('/upload', methods=['post']) def upload(): # name of uploaded file file = request.files['file'] # check if file 1 of allowed types/extensions if file , allowed_file(file.filename): # make filename safe, remove unsupported chars filename = secure_filename(file.filename) # move file form temporal folder # upload folder setup file.save(os.path.join(app.config['upload_folder'], filename)) # redirect user uploaded_file route, # basicaly show on browser uploaded file return redirect(url_for('uploaded_file', filename=filename)) # route expecting parameter containing name # of file. locate file on upload # directory , show on browser, if user uploads # image, image going show after 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) 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 , sold1 == 0 , sold2 ==0): #all 0 return 15 elif (rem == 0 , sold1 == 0 , sold2 < 10): #all 1 product value return sold2*3 elif (rem == 0 , sold1 < 10 , sold2 ==0): return sold1*3 elif (rem < 10 , sold1 == 0 , sold2 == 0): return rem*3 #end 1 product value elif (rem>= 10 , sold1>=10 , sold2>=10): if((rem/3)>=(sold1+10) or (rem/3)>=(sold1+10)): return 0 else: return calculate(rem,sold1,sold2)-rem elif (rem<10 , sold1<10 , sold2<10): return calculate(rem,sold1,sold2) elif (rem == 0 , sold1>=10 , 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 remaining stocks: </h3> <input type="text" name="remaining" /> <br/> <h3>type in stocks past month: </h3> <input type="text" name="sold1" /> <br/> <h3>type in stocks the month before 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
at forecastvalues() wanted evaluate values on each row of csv , evaluate each of values using forecast() , put result of evaluation in 4th column of each row.
so there looped it. , took values turned them integers, assigned them variables rem, sold1, , sold2, , plugged them in forecast(rem, sold1, sold2)
. , assigned return value of forecast result, , put in loop assigned 4th column of row. imagine output this:
from these input
1 2 1 1 3 1 1 2 2
to output when program done file
1 2 1 result 1 3 1 result 1 2 2 result
but seems have no effect on csv file? wrote name of test csv filename test it. there wrong in pandas functions? or have implemented wrong way? used cheatsheet pandas in making code.
you calling pd.to_csv('test.csv')
, not believe work there no pandas.to_csv
method. there pandas.dataframe.to_csv
method, trying accomplish. need call
records.to_csv('test.csv')
Comments
Post a Comment