{"id":3691,"date":"2010-07-25T00:12:46","date_gmt":"2010-07-24T15:12:46","guid":{"rendered":"http:\/\/www.gesource.jp\/weblog\/?p=3691"},"modified":"2017-07-16T15:15:29","modified_gmt":"2017-07-16T06:15:29","slug":"google-app-enginepython%e3%81%a7%e5%8b%95%e7%9a%84%e3%81%abcsv%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e4%bd%9c%e6%88%90%e3%81%97%e3%81%a6%e3%83%80%e3%82%a6%e3%83%b3%e3%83%ad%e3%83%bc%e3%83%89","status":"publish","type":"post","link":"https:\/\/www.gesource.jp\/weblog\/?p=3691","title":{"rendered":"Google App Engine(Python)\u3067\u52d5\u7684\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b"},"content":{"rendered":"<p>Google App Engine(Python)\u3067\u52d5\u7684\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u65b9\u6cd5\u3002<\/p>\n<p>\u4eca\u56de\u306e\u4f8b\u3067\u306f<a href=\"http:\/\/code.google.com\/p\/kay-framework\/\" title=\"kay-framework\">Google App Engine\u7528\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30afKay<\/a>\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>CSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u306b\u306fcsv\u30e2\u30b8\u30e5\u30fc\u30eb\u306eWriter\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre><code>import csv\n\u2026\nwriter = csv.writer(data)\n<\/code><\/pre>\n<p>Writer\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306e\u6700\u521d\u306e\u5f15\u6570\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u30e9\u30a4\u30af\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3068\u308a\u307e\u3059\u3002<br \/>\n\u3053\u3053\u3067\u306fStringIO\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre><code>import StringIO\ndata = StringIO.StringIO()\n<\/code><\/pre>\n<p>csv\u30d5\u30a1\u30a4\u30eb\u306e\u30c7\u30fc\u30bf\u306f\u3001Writer\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306ewriterow()\u3067\u66f8\u304d\u8fbc\u307f\u307e\u3059\u3002<br \/>\n\u5f15\u6570\u306b\u306f\u3001\u884c\u30c7\u30fc\u30bf(\u30d0\u30a4\u30c8\u5217)\u306e\u914d\u5217\u3092\u3068\u308a\u307e\u3059\u3002<br \/>\n\u30e6\u30cb\u30b3\u30fc\u30c9\u6587\u5b57\u5217\u306fencode()\u3067\u30d0\u30a4\u30c8\u5217\u306b\u30a8\u30f3\u30b3\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<pre><code>writer.writerow([u'5560005'.encode('utf-8'), u'\u5927\u962a\u5e9c\u5927\u962a\u5e02\u6d6a\u901f\u533a\u65e5\u672c\u6a4b'.encode('utf-8')])\nwriter.writerow([u'5560006'.encode('utf-8'), u'\u5927\u962a\u5e9c\u5927\u962a\u5e02\u6d6a\u901f\u533a\u65e5\u672c\u6a4b\u6771'.encode('utf-8')])\nwriter.writerow([u'5560004'.encode('utf-8'), u'\u5927\u962a\u5e9c\u5927\u962a\u5e02\u6d6a\u901f\u533a\u65e5\u672c\u6a4b\u897f'.encode('utf-8')])\n<\/code><\/pre>\n<p>\u30d6\u30e9\u30a6\u30b6\u3067\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u306b\u306f\u3001\u30d8\u30c3\u30c0\u306e&#8217;Content-Type&#8217;\u306b&#8217;application\/octet-stream&#8217;\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code>header = Headers()\nheader.add('Content-Type', 'application\/octet-stream');\n<\/code><\/pre>\n<p>\u30d8\u30c3\u30c0\u306e&#8217;Content-Disposition&#8217;\u3067\u3001\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308bCSV\u30d5\u30a1\u30a4\u30eb\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code>header.add('Content-Disposition', 'attachment', filename='sample.csv')\n<\/code><\/pre>\n<p>\u5168\u4f53\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>views.py<\/p>\n<pre><code>def index(request):\n  import csv\n  import StringIO\n  from werkzeug.datastructures import Headers\n  from werkzeug import Response\n\n  data = StringIO.StringIO()\n  writer = csv.writer(data)\n  writer.writerow([u'5560005'.encode('utf-8'), u'\u5927\u962a\u5e9c\u5927\u962a\u5e02\u6d6a\u901f\u533a\u65e5\u672c\u6a4b'.encode('utf-8')])\n  writer.writerow([u'5560006'.encode('utf-8'), u'\u5927\u962a\u5e9c\u5927\u962a\u5e02\u6d6a\u901f\u533a\u65e5\u672c\u6a4b\u6771'.encode('utf-8')])\n  writer.writerow([u'5560004'.encode('utf-8'), u'\u5927\u962a\u5e9c\u5927\u962a\u5e02\u6d6a\u901f\u533a\u65e5\u672c\u6a4b\u897f'.encode('utf-8')])\n\n  header = Headers()\n  header.add('Content-Type', 'application\/octet-stream');\n  header.add('Content-Disposition', 'attachment', filename='sample.csv')\n  return Response(data.getvalue(), headers=header)\n<\/code><\/pre>\n<p>###\u95a2\u9023\u30da\u30fc\u30b8<\/p>\n<ul>\n<li><a href=\"http:\/\/www.python.jp\/doc\/release\/lib\/node267.html\" title=\"Python \u30e9\u30a4\u30d6\u30e9\u30ea\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9 9.1.4 writer \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\">Python \u30e9\u30a4\u30d6\u30e9\u30ea\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9 9.1.4 writer \u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/a><\/li>\n<li><a href=\"http:\/\/www.gesource.jp\/programming\/python\/code\/0003.html\" title=\"CSV\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u3080 - Python Tips\">CSV\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u3080 &#8211; Python Tips<\/a><\/li>\n<\/ul>\n<div class=\"booklink-box\" style=\"text-align:left;padding-bottom:20px;font-size:small;\/zoom: 1;overflow: hidden;\">\n<div class=\"booklink-image\" style=\"float:left;width:110px;margin:0 15px 10px 0;\"><a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/4899772483\/gesource-22\/ref=nosim\/\" name=\"booklink\" rel=\"nofollow\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/ecx.images-amazon.com\/images\/I\/41CvPnjlLZL._SL160_.jpg?w=730\" style=\"border: none;\" \/><\/a><\/div>\n<div class=\"booklink-info\" style=\"line-height:120%;\/zoom: 1;overflow: hidden;\">\n<div class=\"booklink-name\" style=\"margin-bottom:10px;line-height:120%\"><a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/4899772483\/gesource-22\/ref=nosim\/\" rel=\"nofollow\" name=\"booklink\" target=\"_blank\">\u65b0\u4eba\u30d7\u30ed\u30b0\u30e9\u30de\u306e\u305f\u3081\u306eGoogle App Engine\u30af\u30e9\u30a6\u30c9\u30fb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u8b1b\u5ea7\u2015JAVA PYTHON\u5bfe\u5fdc<\/a><\/p>\n<div class=\"booklink-powered-date\" style=\"font-size:8pt;margin-top:5px;font-family:verdana;line-height:120%\">posted with <a href=\"http:\/\/yomereba.com\" target=\"_blank\">\u30e8\u30e1\u30ec\u30d0<\/a> at 10.07.22<\/div>\n<\/div>\n<div class=\"booklink-detail\" style=\"margin-bottom:5px;\">\u638c\u7530 \u6d25\u8036\u4e43 \u30e9\u30c8\u30eb\u30ba 2009-08    <\/div>\n<div class=\"booklink-link2\">\n<div class=\"shoplinkamazon\" style=\"display:inline;margin-right:5px;background: url('http:\/\/farm5.static.flickr.com\/4037\/4697852771_31c83a63a3.jpg') 0 0 no-repeat;padding: 2px 0 2px 18px;white-space: nowrap;\"><a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/asin\/4899772483\/gesource-22\/\" rel=\"nofollow\" target=\"_blank\" title=\"\u30a2\u30de\u30be\u30f3\" >Amazon<\/a><\/div>\n<div class=\"shoplinkrakuten\" style=\"display:inline;margin-right:5px;background: url('http:\/\/farm5.static.flickr.com\/4037\/4697852771_31c83a63a3.jpg') 0 -50px no-repeat;padding: 2px 0 2px 18px;white-space: nowrap;\"><a href=\"http:\/\/pt.afl.rakuten.co.jp\/c\/09af7168.5e4175d8\/?url=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F6164317%2F\" rel=\"nofollow\" target=\"_blank\" title=\"\u697d\u5929\u5e02\u5834\" >\u697d\u5929\u30d6\u30c3\u30af\u30b9<\/a><\/div>\n<div class=\"shoplinkbk1\" style=\"display:inline;margin-right:5px;background: url('http:\/\/farm5.static.flickr.com\/4037\/4697852771_31c83a63a3.jpg') 0 -150px no-repeat;padding: 2px 0 2px 18px;white-space: nowrap;\"><a href=\"http:\/\/ck.jp.ap.valuecommerce.com\/servlet\/referral?sid=2300256&#038;pid=876805418&#038;vc_url=http%3A%2F%2Fwww.bk1.jp%2FkeywordSearchResult%2F%3Fkeyword%3D4899772483\" target=\"_blank\" title=\"bk1\" >bk1<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ad.jp.ap.valuecommerce.com\/servlet\/gifbanner?sid=2300256&#038;pid=876805418\" height=\"1\" width=\"1\" border=\"0\"><\/a><\/div>\n<div class=\"shoplinkbookoff\" style=\"display:inline;margin-right:5px;background: url('http:\/\/farm5.static.flickr.com\/4037\/4697852771_31c83a63a3.jpg') 0 -200px no-repeat;padding: 2px 0 2px 18px;white-space: nowrap;\"><a href=\"http:\/\/click.linksynergy.com\/fs-bin\/click?id=aEiZExlwPrs&#038;subid=&#038;offerid=169505.1&#038;type=10&#038;tmpid=3677&#038;RD_PARM1=http%253A%252F%252Fwww.bookoffonline.co.jp%252Fdisplay%252FL001%252Cbg%253D12%252Cq%253D9784899772484\" rel=\"nofollow\" target=\"_blank\" title=\"\u30d6\u30c3\u30af\u30aa\u30d5\u30aa\u30f3\u30e9\u30a4\u30f3\" >\u30d6\u30c3\u30af\u30aa\u30d5<\/a><\/div>\n<div class=\"shoplinkehon\" style=\"display:inline;margin-right:5px;background: url('http:\/\/farm5.static.flickr.com\/4037\/4697852771_31c83a63a3.jpg') 0 -250px no-repeat;padding: 2px 0 2px 18px;white-space: nowrap;\"><a href=\"http:\/\/ck.jp.ap.valuecommerce.com\/servlet\/referral?sid=2300256&#038;pid=879356331&#038;vc_url=http%3A%2F%2Fwww.e-hon.ne.jp%2Fbec%2FSA%2FDetail%3FrefISBN%3D4899772483\" target=\"_blank\" title=\"e-hon\" >e-hon<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ad.jp.ap.valuecommerce.com\/servlet\/gifbanner?sid=2300256&#038;pid=879356331\" height=\"1\" width=\"1\" border=\"0\"><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Google App Engine(Python)\u3067\u52d5\u7684\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u65b9\u6cd5\u3002 \u4eca\u56de\u306e\u4f8b\u3067\u306fGoogle App Engine\u7528\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30afKay\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002 CSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b &#8230;<\/p>\n<p><a href=\"https:\/\/www.gesource.jp\/weblog\/?p=3691\" class=\"more-link\">Continue reading &lsquo;Google App Engine(Python)\u3067\u52d5\u7684\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b&rsquo; &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[96,95,7],"tags":[68,94,143,42],"class_list":["post-3691","post","type-post","status-publish","format-standard","hentry","category-googleappengine","category-kay-programming","category-python","tag-google-app-engine","tag-kay","tag-python","tag-tips"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/3691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3691"}],"version-history":[{"count":0,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/3691\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}