这方面的新手,所以我可能会遗漏一些简单的东西。我正在尝试创建一个允许用户上传 csv 文档的应用程序,然后我对其进行解析,然后根据 csv 文件中的数据执行其他操作。
我用以下html 设置了一个非常基本的表单。
HTML
<html>
<head>
<title>
Paginator!
</title>
</head>
<body>
<form method="post">
Enter name of page
<br><input type="text" name="pagename">
<br>
Upload page info: <input type="file" name="csvfile" size="chars"><br>
<input type="submit">
</form>
</body>
</html>
Python代码:
import os
import csv
import webapp2
with open('index.html', 'rb') as f:
HTML = f.read()
print HTML
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.write(HTML)
def post(self):
self.response.write(self.request.get('pagename'))
in_file = self.request.get('csvfile')
if in_file:
with open(in_file, 'rb') as f:
csvreader = csv.reader(f, delimiter=',')
data = [row for row in csvreader]
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
所以我加载了带有表单信息的 HTML 文件,并在有 GET 请求时将其写入页面/
。
当我尝试处理 POST 请求时,问题就出现了。当我尝试通过“选择文件”按钮上传任何内容时,我收到来自 GoogleApp Engine 的无此类文件错误。
错误信息:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\Chris\Documents\Code\Python\iqm segmetizer\index.py", line 20, in post
with open(in_file, 'rb') as f:
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 610, in __init__
super(FakeFile, self).__init__(filename, mode, bufsize, **kwargs)
IOError: [Errno 2] No such file or directory: u'test_page_info.csv'
我需要做什么才能允许用户(在这种情况下是我自己)能够通过表单上传 csv 文件?