python Google App Engine:将CSV导入数据存储区

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Google App Engine:将CSV导入数据存储区相关的知识,希望对你有一定的参考价值。

import csv
import webapp2
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import db


class MainHandler(webapp2.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')

        html_string = """
         <form action="%s" method="POST" enctype="multipart/form-data">
        Upload File:
        <input type="file" name="file"> <br>
        <input type="submit" name="submit" value="Submit">
        </form>""" % upload_url

        self.response.write(html_string)


class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]
        process_csv(blob_info)

        blobstore.delete(blob_info.key())  # optional: delete file after import
        self.redirect("/")


def process_csv(blob_info):
    blob_reader = blobstore.BlobReader(blob_info.key())
    reader = csv.reader(blob_reader, delimiter=';')
    for row in reader:
        date, data, value = row
        entry = EntriesDB(date=date, data=data, value=int(value))
        entry.put()


class EntriesDB(db.Model):
    date = db.DateProperty()
    data = db.StringProperty()
    value = db.IntegerProperty()

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/upload', UploadHandler)
], debug=True)

以上是关于python Google App Engine:将CSV导入数据存储区的主要内容,如果未能解决你的问题,请参考以下文章

python 将Google App Engine SDK添加到Python virtualenv

python Google App Engine:将CSV导入数据存储区

google-app-engine:google api python客户端hello world中的ImportError httplib2

Google App Engine 标准 Python 云构建

Google App Engine - 大查询 - Python 找不到库 google.cloud

Python 2.5 Google App Engine [关闭]