TypeError:convert()缺少1个必需的位置参数:' filename'

时间:2018-03-12 23:04:50

标签: python file-upload flask typeerror

我刚开始使用python。 我的目标是使用flask来构建一个程序,它通过文件上传作为输入获取csv文件,在csv上执行代码并返回一个新的csv。

每当我尝试运行代码时,上传文件时总是会出现以下错误:

  

TypeError:convert()缺少1个必需的位置参数:   '文件名'

我的代码如下:



import os
from flask import Flask, render_template, redirect, request, send_from_directory, url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)
ALLOWED_EXTENSIONS = set(["csv"])

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/process', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected 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('convert', filename=filename))

@app.route("/convert")
def convert(filename):
    with open (UPLOAD_FOLDER + filename, "r", encoding="UTF-8") as file:
        umlaute_dict = {
            'ä': 'ae',  # U+00E4	   \xc3\xa4
            'ö': 'oe',  # U+00F6	   \xc3\xb6
            'ü': 'ue',  # U+00FC	   \xc3\xbc
            'Ä': 'Ae',  # U+00C4	   \xc3\x84
            'Ö': 'Oe',  # U+00D6	   \xc3\x96
            'Ü': 'Ue',  # U+00DC	   \xc3\x9c
            'ß': 'ss',  # U+00DF	   \xc3\x9f
            '&#x2F': '',
        }
    with open(UPLOAD_FOLDER + 'csvfile.csv', 'w') as filetwo:
        for row in file:
            final = " "
            list = row.split(",")
            line = []
            firstname = list[5]
            for k in umlaute_dict:
                firstname = firstname.replace(k, umlaute_dict[k])
            title = list[8]
            for k in umlaute_dict:
                title = title.replace(k, umlaute_dict[k])
            line.append(firstname + ",")
            line.append(list[7] + ",")
            line.append(title + ",")
            line.append(list[9] + ",")
            final = "".join(line)
            print (final)
            filetwo.write(final)
            filetwo.write('\n')
            return redirect(url_for('uploaded_file', filename=filetwo))

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


app.run(debug=True)
&#13;
&#13;
&#13;

感谢您的帮助!!

最佳

1 个答案:

答案 0 :(得分:2)

为什么你认为你的转换会收到文件名参数?

这里缺少一些东西

@app.route("/convert")
def convert(filename):

也许您希望在

中遵循相同的代码模式
@app.route('/uploads/<filename>')
def uploaded_file(filename):