Home

Published

- 1 min read

Google App Engine: Import CSV to Datastore

img of Google App Engine: Import CSV to Datastore

Processing CSV Data with the Google App Engine is a two-step process. First, you need to upload the data and store it in the blob store. Then you retrieve the file and process the information and save it to the Datastore(Database). You can process the CSV data with the standard python tools from the CSV-module, however, instead of parsing a file you must parse a BlobReader.

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)