Not able to parse a .csv file uploaded using Flask

后端 未结 2 1037
萌比男神i
萌比男神i 2020-11-29 03:35

I am trying to upload a CSV file, work on it to produce results, and write back (download) a new CSV file containing the result. I am very new to Flask and I am not able to

2条回答
  •  误落风尘
    2020-11-29 04:14

    OK, so there is one major problem with your script, csv.reader as noted here expects a file object or at least an object which supports the iterator protocol. You are passing a str which does implement the iterator protocol, but instead of iterating through the lines, it iterates through the characters. This is why you have the output you do.

    First, it gives a singe character 1 which the csv.reader sees as a line with one field. After that the str gives another single character , which the csv.reader sees as a line with two empty fields (since the comma is the field seperator). It goes on like that throughout the str until it's exhausted.

    The solution (or at least one solution) is to turn the str into a file-like object. I tried using the stream provided by flask.request.files["name"], but that doesn't iterate through the lines. Next, I tried using a cStringIO.StringIO and that seemed to have a similar issue. I ended up at this question which suggested an io.StringIO object in universal newlines mode which worked. I ended up with the following working code (perhaps it could be better):

    __author__ = 'shivendra'
    from flask import Flask, make_response, request
    import io
    import csv
    
    app = Flask(__name__)
    
    def transform(text_file_contents):
        return text_file_contents.replace("=", ",")
    
    
    @app.route('/')
    def form():
        return """
            
                
                    

    Transform a file demo

    """ @app.route('/transform', methods=["POST"]) def transform_view(): f = request.files['data_file'] if not f: return "No file" stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None) csv_input = csv.reader(stream) #print("file contents: ", file_contents) #print(type(file_contents)) print(csv_input) for row in csv_input: print(row) stream.seek(0) result = transform(stream.read()) response = make_response(result) response.headers["Content-Disposition"] = "attachment; filename=result.csv" return response if __name__ == "__main__": app.run(host='0.0.0.0', port=5001, debug=True)

提交回复
热议问题