Opening a file that has been uploaded in Flask

后端 未结 2 1436
余生分开走
余生分开走 2020-12-22 00:00

I\'m trying to modify a csv that is uploaded into my flask application. I have the logic that works just fine when I don\'t upload it through flask.

import p         


        
相关标签:
2条回答
  • 2020-12-22 00:26

    FileStorage is a file-like wrapper around the incoming data. You can pass it directly to read_csv.

    pd.read_csv(request.files['data_file'])
    

    You most likely should not be performing those replace calls on the data, as the CSV module should handle that and the naive replacement can corrupt data in quoted columns. However, if you still need to, you can read the data out just like you were before.

    data = request.files['data_file'].read()
    

    If your data has a mix of quoting styles, you should fix the source of your data.

    0 讨论(0)
  • 2020-12-22 00:27

    Answering my own question in case someone else needs this.

    FileStorage objects have a .stream attribute which will be an io.BytesIO

    f  = request.files['data_file']
    df = pandas.read_csv(f.stream)
    
    0 讨论(0)
提交回复
热议问题