如何将表单数据写入文本文档

时间:2016-12-14 09:12:54

标签: python wtforms

我正在尝试将我的表单数据捕获并写入word文档。我有以下功能...

def index():
    FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd'])
    mail = Mail(app)
    errors = '' 
    form = ApplicationForm(CombinedMultiDict((request.files, request.form)))
    capture = [form.department.data, form.name.data, form.address.data, str(form.telephone.data), form.email.data, form.explain.data]
    department_data = form.department.data
    name_data = form.name.data
    if department_data == 'cpoms':
        flash(capture)
    if form.validate_on_submit():
        flash('Thanks %s, we will try to get back to your regarding you application as soon as possible.' % form.name.data) 
        print "Form successfully submitted"
        submit_name = form.file_upload.data.filename
        if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
            filename = secure_filename(submit_name)
            form.file_upload.data.save('uploads/' + filename)
            return redirect('home')

有谁知道如何从捕获变量中移动数据并将其写入文本文档?

1 个答案:

答案 0 :(得分:0)

怎么样:

with open("/path/to/file.txt", "a") as write_file:
    for cap in capture:
        write_file.write(capt)

我希望这会奏效。