[Flask] Uploading CSV file to flask, only first line being uploaded. Help ! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: [Flask] Uploading CSV file to flask, only first line being uploaded. Help ! (/thread-11149.html) |
Uploading CSV file to flask, only first line being uploaded. Help ! - KirkmanJ - Jun-25-2018 @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. RE: Uploading CSV file to flask, only first line being uploaded. Help ! - buran - Jun-25-2018 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 RE: Uploading CSV file to flask, only first line being uploaded. Help ! - KirkmanJ - Jun-25-2018 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. |