我正在尝试做一个web API来保存一个csv到一个模板excel文件。我已经制作了一个函数,当我执行它时,它工作得很完美。但是一旦我从函数(def on_get(self,req,resp):)调用它,文件就会被破坏。为了防止文件损坏,我可以做哪些更改?
服务器代码:
class Test:
def on_get(self, req, resp):
ct.save_df_into_excel(df_template, file_location, list_of_code_columns, new_location)
"""Handles GET requests"""
response = {
'status': 'success'
}
resp.media = response
功能:
def save_df_into_excel(df_template, file_location, list_of_code_columns, new_location = None):
if new_location == None:
new_location = file_location
if df_template.columns.nlevels > 1:
df_template.columns = df_template.columns.droplevel()
df_new_template = df_template.reindex(columns=list_of_code_columns)
workbook = openpyxl.load_workbook(file_location)
writer = pd.ExcelWriter(file_location, engine='openpyxl')
writer.book = workbook
writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)
worksheet = workbook.active
df_new_template.to_excel(writer, sheet_name=worksheet.title,
header=None, startrow=2, index=False)
writer.book.save(new_location)
发布于 2020-06-27 21:41:34
根据我的经验,使用falcon处理传入文件的最好方法是使用falcon-multipart中间件,您可以从https://github.com/yohanboniface/falcon-multipart获取它,并像这样使用它:
from falcon_multipart.middleware import MultipartMiddleware
api = falcon.API(middleware=[MultipartMiddleware()])
然后在你的端点中使用类似这样的东西:
class UploadHandler():
def on_post(self, req, resp):
iFile = req.get_param('sentFile')
https://stackoverflow.com/questions/56853397
复制相似问题