python - 如何访问客户端存在的数据文件并进行处理?
问题描述
我想要达到的目标如下:
- 设计一个从客户端接收数据、处理数据然后打印一个小表格的 API。
如何访问文件数据?- 我要求客户向我发送文件的位置,但这不起作用。请提出合适的方法。
API 的代码如下:
import pandas as pd
from flask import Flask, request,render_template
import os
app = Flask(__name__)
@app.route('/<path:fileN>', methods=['GET'])
def my_form_post(fileN):
f=open(fileN,"r")
#f=os.read(fileN,os.O_RDONLY)
lines=f.readlines()
ff=[]
for line in lines:
parts=line.split("\t")
parts=parts[:7]
ff.append(parts)
pd.set_option("display.colheader_justify","center")
df=pd.DataFrame(ff,columns=['Date', 'Start Time', 'Time', '# of Req', 'HTTP Method', 'Link','HTTP Response'])
df=df[df['Link'].str.startswith('/ic/')==True]
df.sort_values(by="Time", axis=0, ascending=False, inplace=True)
df.reset_index(inplace=True)
df.drop(['index'], axis=1, inplace=True)
df.index+=1
df=df.head(5)
html=df.to_html()
return html
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True, debug=True)
解决方案
要接收文件,您需要使用 POST 方法并从请求中检索文件,如下所示:
file = request.files['file']
请注意,仅当请求方法为 POST、PUT 或 PATCH 并且发布到请求的文件具有 enctype="multipart/form-data" 时,文件才会包含数据。否则它将为空。您可以在此处查看更多详细信息。
推荐阅读
- java - 启动 Spring Boot 示例应用程序时出现问题
- javascript - 使用 javascript/jquery 从字符串元素中删除内联样式
- c# - 从xml文件c#中提取嵌套节点
- python - 自定义损失函数,它依赖于 keras 中的另一个神经网络
- camera - 当我添加 QRScanner 时,Ionic App 显示空白屏幕
- excel-formula - Power BI - 我需要一个“countif”公式来查看行/列中的唯一条目
- excel - 检查word文件是否已经打开vba
- android - 如何在 MobiWire MobiPrint 3 (Android) 上打印
- grails - Grails - 禁用默认 url 映射
- json - 如何在 Swift 中创建嵌套的 [String: Anyobject]?