I'm currently making an api with flask to upload a CSV file and upload the data in CSV to postgresql through sqlalchemy(using flask_sqlalchemy)
here the CSV file example:
text,phonetic_symbols,fgner_entity,user_id,data_type_id,sns_raw_data_id
hhhh,jj,kk,3,2,1
ll,hh,eee,3,2,1
When i first wrote the quote to upload through file it worked and the data got uploaded
Original code
@app.route('/upload', methods=['POST'])
def upload_csv():
if 'file' not in request.files:
return jsonify('No file part')
file = request.files['file']
if file.filename == '':
return jsonify(msg='no file selected')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f:
next(f)
csv_file = csv.reader(f, delimiter=',')
for line in csv_file:
new_dictionary_item = DictionaryItem(text=line[0], phonetic_symbols=line[1], fgner_entity=line[2],
user_id=line[3], data_type_id=line[4], sns_raw_data_id=line[5])
db.session.add(new_dictionary_item)
db.session.commit()
return jsonify("update sucess"), 200
return jsonify(msg='not correct file type'), 400:
Now i need to add a condition to check if every lines in the CSV file has exactly 6 fields to upload data
I wrote it within an if condition to check. The condition worked and return the appropriate response but the data doesn't get upload to my database anymore
New code with "if" condition:
@app.route('/upload', methods=['POST'])
def upload_csv():
if 'file' not in request.files:
return jsonify('No file part')
file = request.files['file']
if file.filename == '':
return jsonify(msg='no file selected')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f:
next(f)
csv_file = csv.reader(f, delimiter=',')
if all(len(single_line) == 6 for single_line in csv_file):
for line in csv_file:
new_dictionary_item = DictionaryItem(text=line[0], phonetic_symbols=line[1], fgner_entity=line[2],
user_id=line[3], data_type_id=line[4], sns_raw_data_id=line[5])
db.session.add(new_dictionary_item)
db.session.commit()
return jsonify("update sucess"), 200
return jsonify("not correct csv format"), 400
return jsonify(msg='not correct file type'), 400
Response update sucess but data doesn't get uploaded
What am i doing wrong ? Thank
EDIT:
Thank shrumm for telling me what wrong with my code and helped me get it to work. I would suggest to use psycopg2 as it would be more optimize but my project already using sqlalchemy so this will do:
@app.route('/upload', methods=['POST'])
def upload_csv():
if 'file' not in request.files:
return jsonify('No file part')
file = request.files['file']
if file.filename == '':
return jsonify(msg='No file selected')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f:
csv_file = csv.reader(f, delimiter=',')
next(f)
if all(len(single_line) == 6 for single_line in csv_file):
f.seek(0)
next(f)
for line in csv_file:
new_dictionary_item = DictionaryItem(text=line[0], phonetic_symbols=line[1], fgner_entity=line[2],
user_id=line[3], data_type_id=line[4], sns_raw_data_id=line[5])
db.session.add(new_dictionary_item)
db.session.commit()
return jsonify("Update succeed"), 200
else:
return jsonify("Not correct csv format"), 400
return jsonify(msg='Not correct file type'), 400