2017-04-14 3 views
0

こんにちは、私はWebアプリケーション開発者を新しくしています。私はcsvファイルを操作するアプリケーションを作ろうとしています。 )(私はcsvファイルの各行の値を評価し、予測を使用してこれらの値のそれぞれを評価したかった)forecastvaluesでPythonはパンダを使用してシート上の値を評価するファイルを読み込んで書き込む

#!/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 

(:

私は以下の私の問題を述べる前に、私は私のコードを貼り付けてみましょう各行の4列目に評価結果を記入してください。

そこで私はそれを繰り返しました。値を整数に変換し、変数rem、sold1、およびsold2に割り当て、forecast(rem, sold1, sold2)に接続しました。そして、予測の戻り値をresultに代入し、それをループに入れて行の4番目の列に代入できるようにしました。プログラムがファイルで行われたときに

1 2 1 result 
1 3 1 result 
1 2 2 result 

この出力にこれらの入力

1 2 1 
1 3 1 
1 2 2 

から

しかし、それは影響を与えませんようだ:私は、出力は次のようであることを想像しますCSVファイルですか?私はファイル名としてテストcsvの名前を書いてテストしました。私のパンダの機能に何か間違っていますか?私はそれを間違った方法で実装していますか?私はパンダのチートシートを使ってコードを作った。

答えて

0

pd.to_csv('test.csv')に電話していますが、pandas.to_csvメソッドが存在しないため、これが機能するとは思われません。しかし、あなたが達成しようとしていることを行うpandas.DataFrame.to_csvメソッドがあります。あなたはそれを指摘するために

records.to_csv('test.csv') 
+0

ハロー感謝を呼び出す必要があり、私はそれを更新し、しかし、私は、このトレースバックました: に 'ファイル「/home/Firiyuu77/mysite/flask_app.py」、ライン56を、デバッグ= True ファイル "/usr/local/lib/python2.7/dist-packages/flask/app.py"、行843、実行中 run_simple(ホスト、ポート、自己、**オプション) ファイル "/usr/local/lib/python2.7/dist-packages/flask/app.py" run_simple s.bind((ホスト名、ポート)) ファイル "/usr/lib/python2.7/ソケット/ usr/local/lib/python2.7/dist-packages/werkzeug/serving.py" .py "、224行目、methで getattr(self._sock、name)(* args) ' – dekt

+0

それは完全なエラーですか? – Grr

+0

私のループを正しく実行したと思いますか? – dekt

関連する問題