Python Forum
[Flask] Uploading CSV file to flask, only first line being uploaded. Help !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Flask] Uploading CSV file to flask, only first line being uploaded. Help !
#1
@upload_csv_blueprint.route('/upload_csv', methods=['GET','POST'])
def upload_file():
    if request.method == 'POST':
        csvfile = request.files['file']
        reader = csv.DictReader(csvfile)
        data = [row for row in reader]
        for row in data:
            date_time_store = row['date_time']
            technician_store = row['technician']
            test_location_store = row['test_location']
            product_serial_number_store = row['product_serial_number']
            test_detail_store = row['test_detail']
            test_result_store = row['test_result']

            query = test_result(date_time = date_time_store,
                                technician_name = technician_store,
                                place_of_test = test_location_store,
                                serial_number=product_serial_number_store,
                                test_details=test_detail_store,
                                result=test_result_store)

            db.session.add(query)
            db.session.commit()
            return('Did it work?')
        else:
            return redirect(url_for('upload_csv.upload_csv_layout'))#
The code above is my current version. This version uploads the second line of the csv file ( the first line being the headings used to match up the columns.)
The csv files i will be uploading could contain up too 1000 lines of data. I was hoping the method I had constructed would loop and add all 1000 entries. But it appears to just quit after the first.

I am constructing my project on a Flask platform, using sql alchemy. If anyone knows where my error lies or knows of an easy way to upload a csv file into a sql database. Would be really appreciated as I have spent the better part of a 2 days searching for the answer.
Reply
#2
I think lines#24-26 should be unindented one level. At the moment you will return after first line.
Also you can iterate over reader, no need to construct data list
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Im in two minds, half of me wants to give you a hug the other half wants to slap you because im annoyed it took me 2 days and i could not figure out that i just needed to hit backspaces twice :).

Thanks, huge help.

To clarify: unindenting lines 23- 26 by 1 solves the issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  show csv file in flask template.html rr28rizal 8 34,732 Apr-12-2021, 09:24 AM
Last Post: adamabusamra
  Uploading images from multipart/data/-from get corrupted jdc18 0 1,936 Sep-30-2020, 07:16 PM
Last Post: jdc18
  flask How to output stderr and exceptions to log file umen 4 4,775 Jun-20-2020, 06:11 AM
Last Post: umen
  Read owl file using python flask Gayathri 1 2,445 Nov-20-2019, 12:56 PM
Last Post: ChislaineWijdeven
  Create .exe file for Python flask website. vintysaw 4 19,186 Nov-18-2019, 07:56 AM
Last Post: tonycstech
  Flask generating a file for download darktitan 0 3,347 Dec-30-2018, 02:02 PM
Last Post: darktitan
  How to upload images after first paragraph ends instead of uploading them at the end? SamLearnsPython 0 1,951 Sep-15-2018, 06:53 AM
Last Post: SamLearnsPython
  Uploading multiple pdfs to Flask ( Sql alchmey) with additional variables KirkmanJ 10 9,483 Jul-27-2018, 09:49 AM
Last Post: KirkmanJ
  How to save uploaded image url in database from Django? PrateekG 14 14,870 Jul-04-2018, 05:18 PM
Last Post: PrateekG
  How to perform predictions on a data in csv file using Flask? manjusavanth 0 4,515 May-23-2018, 09:03 AM
Last Post: manjusavanth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020