Not able to parse a .csv file uploaded using Flask

后端 未结 2 1015
萌比男神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条回答
  •  -上瘾入骨i
    2020-11-29 04:21

    Important note: This answer is relevant only for platforms where SpooledTemporaryFile is available.

    Further to iLuveTux answer, you can save the redundant read() call by replacing the following string-based stream creation:

    stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None)
    

    with:

    stream = io.TextIOWrapper(f.stream._file, "UTF8", newline=None)
    

    Example:

    stream = io.TextIOWrapper(f.stream._file, "UTF8", newline=None)
    csv_input = csv.reader(stream)
    print(csv_input)
    for row in csv_input:
        print(row)
    

    Further information:

    Werkzeug default stream for form data parser is SpooledTemporaryFile (as of 1.0.1), from which you can obtain the underlying buffer using its _file memeber.

提交回复
热议问题