如何在App Engine中上传和读取文件

时间:2014-08-04 14:22:25

标签: python google-app-engine blobstore

我有一个上传HTML格式:

<form method="post" enctype="multipart/form-data" action="{{ upload_url }}">
    <input type="file" id="import_groups_csv" name="import_groups_csv">
    <input type="submit" value="Import groups">
</form>

这是从blobstore读取文件的类:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('import_groups_csv')  # 'file' is file upload field in the form
        blob_info = upload_files[0]
        file = blobstore.get(blob_info.key()).open().read()
        for line in file:
            logging.info("line *******************"+str(line))
        self.redirect('/groups')

当文件上传时,我想读取它上面的每一行但是这个我git已经是一个错误,这是追溯:

INFO     2014-08-04 13:47:04,252 views.py:332] line *******************ERROR    2014-08-04 13:47:04,255 traceback.py:13] Traceback (most recent call last):

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\app_logging.py", line 78, in emit

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     logservice.write(message)

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice.py", line 347, in write

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     logs_buffer().write(message)

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice.py", line 236, in write

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     return self._lock_and_call(self._write, line)

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice.py", line 157, in _lock_and_call

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     return method(*args)

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice.py", line 253, in _write

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     self._autoflush()

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice.py", line 321, in _autoflush

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     self._flush()

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice.py", line 307, in _flush

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     apiproxy_stub_map.MakeSyncCall('logservice', 'Flush', request, response)

ERROR    2014-08-04 13:47:04,256 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 94, in MakeSyncCall

ERROR    2014-08-04 13:47:04,256 traceback.py:13]     return stubmap.MakeSyncCall(service, call, request, response)

ERROR    2014-08-04 13:47:04,257 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 320, in MakeSyncCall

ERROR    2014-08-04 13:47:04,257 traceback.py:13]     rpc.CheckSuccess()

ERROR    2014-08-04 13:47:04,257 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_rpc.py", line 156, in _WaitImpl

ERROR    2014-08-04 13:47:04,257 traceback.py:13]     self.request, self.response)

ERROR    2014-08-04 13:47:04,257 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub.py", line 123, in MakeSyncCall

ERROR    2014-08-04 13:47:04,257 traceback.py:13]     method(request, response, request_id)

ERROR    2014-08-04 13:47:04,257 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice_stub.py", line 182, in _Dynamic_Flush

ERROR    2014-08-04 13:47:04,257 traceback.py:13]     self._insert_app_logs(request_id, group.log_line_list())

ERROR    2014-08-04 13:47:04,257 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub.py", line 160, in WrappedMethod

ERROR    2014-08-04 13:47:04,259 traceback.py:13]     return method(self, *args, **kwargs)

ERROR    2014-08-04 13:47:04,259 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice_stub.py", line 193, in _insert_app_logs

ERROR    2014-08-04 13:47:04,259 traceback.py:13]     '(?, ?, ?, ?)', new_app_logs)

ERROR    2014-08-04 13:47:04,259 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice_stub.py", line 190, in <genexpr>

ERROR    2014-08-04 13:47:04,259 traceback.py:13]     for log_line in log_lines)

ERROR    2014-08-04 13:47:04,259 traceback.py:13]   File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\logservice\logservice_stub.py", line 200, in _tuple_from_log_line

ERROR    2014-08-04 13:47:04,259 traceback.py:13]     message = message.decode('utf-8')

ERROR    2014-08-04 13:47:04,259 traceback.py:13]   File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode

ERROR    2014-08-04 13:47:04,259 traceback.py:13]     return codecs.utf_8_decode(input, errors, True)

ERROR    2014-08-04 13:47:04,259 traceback.py:13] UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 27: invalid start byte

ERROR    2014-08-04 13:47:04,259 app_logging.py:82] Logged from file views.py, line 332

我按照谷歌开发者的说法进行了跟踪,但是文档记录很差。任何根本原因的想法?

1 个答案:

答案 0 :(得分:1)

将其作为views.py中的第一行:

# -*- coding: utf-8 -*-

并确保views.py以UTF-8格式保存。根据文本编辑器的不同,转到“另存为”,并确保它是Unicode,而不是ASCII。