使用Python和Flask为什么我上传一个csv文件时会出现缩进错误?

时间:2015-11-07 18:59:30

标签: python flask postgresql-9.3

我想上传一个csv文件,打开它然后将每一行插入我的postgres数据库。这是代码:

@app.route("/uploadcsv", methods=['GET', 'POST'])
def uploadcsv():
    if request.method == 'POST' and 'photo' in request.files:
        csvfile = open(request.form['myfile'], 'r')
        sql = Prospect(first_name=row[first_name], last_name=row[last_name], email = row[email], phone=row[phone], designation=row[designation], company=row[company], industry=row[industry], tag=row[tag], created_at=row[created_at]) 
            db.session.add(sql)
        db.session.commit()
        #flash("Photo saved.")
    return render_template('upload.html')

我收到缩进错误。 heroku日志说:

IndentationError: unexpected indent
2015-11-07T18:52:19.650383+00:00 app[web.1]:     sys.exit(run())
2015-11-07T18:52:19.623486+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
2015-11-07T18:52:19.623487+00:00 app[web.1]:     worker.init_process()
2015-11-07T18:52:19.623489+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2015-11-07T18:52:19.623490+00:00 app[web.1]:     self.callable = self.load()
2015-11-07T18:52:19.623495+00:00 app[web.1]:     db.session.add(sql)

我尝试了多次不同的缩进尝试但没有工作。我在这做错了什么?感谢所有的帮助。谢谢。

1 个答案:

答案 0 :(得分:3)

            db.session.add(sql)

该行代码不应缩进。