简体   繁体   中英

how to open and iterate through csv file having data from POST request from website

So im making simple web with currency converter. I have saved currencies in csv file named 'out.csv' which looks like this:

currency;code;bid;ask
dolar amerykanski;USD;3.9437;4.0233
dolar australijski;AUD;2.9158;2.9748
dolar kanadyjski;CAD;3.1679;3.2319
euro;EUR;4.553;4.645

I do get data that user had wrote in the form with request method.

from flask import Flask, render_template, redirect, request
app= Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def kantor():
    if request.method == 'GET':
        return render_template('kantor.html')
    elif request.method == 'POST':
        waluta= request.form['waluta']
        amount = request.form['amount']
        return render_template('wynik.html', amount=amount, waluta=waluta)

And now my problem is how do i calculate it. Lets say someone chose 100 EUR and i have to print in another html page how much does it cost. So 100 * EUR 'bid' price from csv file = X.

I dont have any clue how can I iterate through csv file having only chose 'code' so i can obtain its 'bid' price and then do the math and print result. I would appreciate any help or advice how can I make it

Like has been said in the comments, you have to use pandas. Yor firts step would be:

df = pd.read_csv("path/to/file.csv")

and you will easily find a lot of tutorials to help you with the rest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM