昨日とは反対に、今度は、CSVファイルをGoogle App Engineのほうにアップロードして、データベースに格納します。
CSVファイルの仕様は昨日とほぼ一緒ですが、ヘッダ行は入れません。
#!/usr/bin/env python # -*- coding: UTF-8 -*- import wsgiref.handlers import os import csv from StringIO import StringIO from google.appengine.ext import webapp, db from google.appengine.ext.webapp import template class Message(db.Model): username = db.StringProperty() phrase = db.StringProperty() class CsvUploader(webapp.RequestHandler): def get(self): template_values = { 'messages': Message.all(), } path = os.path.join(os.path.dirname(__file__), "main.html") self.response.out.write(template.render(path, template_values)) def post(self): rawfile = self.request.get('file') csvfile = csv.reader(StringIO(rawfile)) for row in csvfile: m = Message( username = unicode(row[0], 'cp932'), phrase = unicode(row[1], 'cp932') ) m.put() self.redirect(self.request.uri) def main(): application = webapp.WSGIApplication([ ('/', CsvUploader) ],debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CSVファイルのアップロード</title> </head> <body> <table><tr><th>発言者</th><th>発言コメント</th></tr> {% for m in messages %} <tr><td>{{m.username}}</td><td>{{m.phrase}}</td></tr> {% endfor %} </table> <hr /> <form method="post" action="#" enctype="multipart/form-data"> ファイル: <input type="file" name="file" size="30" /> <input type="submit" value="アップロード" /> </form> </body> </html>
もーさすがに3回は果てるってーー!!!(>_<)
連続じゃないだけマシだけど1 0 万の為とはいえ3回ヤるとティ ンコさんが火を噴きそうなくらい真っ赤っ赤だよ(^^;
まー何気に足 コ キしてもらったのって初めてだし、得っちゃ得だけどねーwww
http://kachi.strowcrue.net/IPVP137/