尝试在Flask上处理csv后,没有可从文件解析的列

时间:2019-04-26 20:14:19

标签: python pandas flask

我正在尝试在烧瓶上处理csv文件,以进行清理。我知道实际的清理脚本可以正常工作。但是,我是flask的新手,由于某种原因,当我运行代码时,会收到错误消息:

  

pandas.errors.EmptyDataError pandas.errors.EmptyDataError:没有列   从文件中解析

这是我正在为此使用的代码

import os
import pandas as pd
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = './Downloads/gmbreports'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

ALLOWED_EXTENSIONS = 'csv'

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('You need to upload a csv file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Google My Business Discovery Report Builder</title>
    <h1>Upload GMB Discovery csv</h1>
    <form method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''
from flask import send_from_directory

#once you upload the file you get cleaned up visu
@app.route('/uploads/<filename>')
def uploaded_file(filename):
    path = UPLOAD_FOLDER + "/" + filename
    file_one = open(path)
    file_two = open(path,'w')
    for row in file_one:
        row = row.strip()
        row = row[1:-1]
        row = row.replace('""','"')
        file_two.write(row+'\n')
    file_two.close()
    file_one.close()
    discovery=pd.read_csv(path)
    return discovery
    discovery_clean=discovery.iloc[1:]
    cols=list(discovery_clean.columns[4:])
    discovery_clean[cols]=discovery_clean[cols].apply(pd.to_numeric,errors='coerce')
    return discovery_clean

if __name__=='__main__':
    app.run(debug=True)

我还附上了我要清理的数据框示例 Sample File

0 个答案:

没有答案