我希望用户上载选定的csv文件并发送到服务器-flask,我想在其中对csv文件进行处理,然后将其作为表格发送回去供用户编辑。
我能够将上载的文件保存到flask并激活python代码作为一个函数,但是一旦我在第一行使用上载的文件(必须更改文件)后,它就会给我一个错误: AttributeError:“ SpooledTemporaryFile”对象没有属性“重命名” 我被困住了,因为我还不熟悉烧瓶。
这是应该编辑上传文件的route.py部分:
@login_required
def open_file():
'''Opens page with the file that should be edited.'''
if request.method == 'POST':
#check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return render_template('2_choose_file.html')
file_upload = request.files['file']
#check if the uploaded file a CSV file is
if file_upload and allowed_file(file_upload.filename):
table1 = filter_csv(file_upload)
table2 = table1.to_html(classes='my_class" id = "my_id')
return render_template('3_filtered_file.html', data=table2)
return render_template('2_choose_file.html')
错误的跟踪:
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_login\utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "C:\Users\aa01725\newproject\my_app\routes.py", line 83, in open_file
table1 = filter_csv(file_upload)
File "C:\Users\aa01725\newproject\my_app\TEST_filter.py", line 42, in filter_csv
file_in.rename(columns=dict(zip(columns, new_column_names)), inplace=True)
File "C:\Users\aa01725\AppData\Local\Continuum\anaconda3\lib\site-packages\werkzeug\datastructures.py", line 2745, in __getattr__
return getattr(self.stream, name)
AttributeError: 'SpooledTemporaryFile' object has no attribute 'rename'
显示错误的python代码的一部分。
file_in.rename(columns=dict(zip(columns, new_column_names)), inplace=True)
任何帮助都会很棒
答案 0 :(得分:0)
我找到了解决方法。基于this answer。只需将上传的文件保存到服务器,然后使用pd.read_csv解析它,然后再次使用即可。
@app.route('/3_filtered_file', methods=['GET', 'POST'])
@login_required
def open_file():
'''Opens page with the file that was imported from the user.'''
if request.method == 'POST':
#check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return render_template('2_choose_file.html')
file_upload = request.files['file']
#check if the uploaded file a CSV file is
if file_upload and allowed_file(file_upload.filename):
filename = secure_filename(file_upload.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file_upload.save(file_path)
file_to_filter = pd.read_csv(file_path, sep=';', engine='python', encoding='ISO-8859-1')
table1 = filter_csv(file_to_filter)
table2 = table1.to_html(classes='my_class" id = "my_id')
return render_template('3_filtered_file.html', data=table2)
return render_template('2_choose_file.html')