0

Hello everyone i will send from my view a csv file to the flask backend and open it to do some preprocessing, can anyone show me how to do it please ,

i do some code but it doesn't work

    filename = request.files['csv']
    with open(filename, "rt") as csv_file:
        content = csv.reader(csv_file,delimiter=';') 

view

<form  method=post action="{{ url_for('tables') }}" enctype=multipart/form-data 
                    <input type=file name=csv id="tables"   required/> 
                        <button >
                            Lancer l’IA de vision machine
                        </button>    
</form>
love python
  • 59
  • 2
  • 8
  • Out of interest, why is your `open` with `rt` flags? Try using `rb` instead. Looks like the `open` part doesn't work because it's not yet saved. You'll potentially need `StringIO` or `BytesIO` to read it. – jayg_code Nov 05 '19 at 10:31
  • "doesn't work" is the most useless information. If you get error then show it in question. always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Nov 05 '19 at 10:41
  • first use `print(filename)` to see what you get. And you will see that `request.files['csv']` gives something different then filename. OR simply find some tutorial and read it . ie. [Uploading Files](https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/) – furas Nov 05 '19 at 10:43

1 Answers1

0

If you are trying to read a remote file. Then follow this

Read csv from url

or use 'pandas' library

import pandas as pd
df = pd.read_csv(url)#works for url too.