使用Pandas将原始文件内容从Flask文件上传转换为数据帧 [英] Converting raw file content from Flask file upload into dataframe using pandas
问题描述
我正在尝试将csv
文件上传到我的烧瓶服务器.我要做的是将其内容读入dataframe
而不将其保存在文件系统上.现在,我正在使用file.read()
方法来获取文件的内容,但是当我将这些内容转换为熊猫dataframe
时,我感到茫然.这是代码:
I am trying to upload a csv
file to my flask server. What I want to do is to read its content into a dataframe
without saving it on the file system. For now I'm using the file.read()
method to get the contents of the file, but I'm at loss when it comes to converting these content into a pandas dataframe
. Here's the code:
@app.route('/upload', methods=['POST'])
def upload():
file = request.files.get('uploaded_file')
filename = secure_filename(file.filename)
file_content = file.read()
# want to convert these file content into a pandas dataframe
保存到磁盘时,我可以将其作为数据帧加载,但是我想解析内容而不保存上载的文件.
I am able to load it as a dataframe when saving to the disk, but I want to parse the content without saving the uploaded file.
推荐答案
pandas.read_csv()
可以将任何类似文件的对象(使用read()
方法)作为输入,因此只需使用它即可:
pandas.read_csv()
can take any file-like object (with a read()
method) as input, so just use it: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html#pandas.read_csv
@app.route('/upload', methods=['POST'])
def upload():
df = pandas.read_csv(request.files.get('uploaded_file'))
这篇关于使用Pandas将原始文件内容从Flask文件上传转换为数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!