<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Love web applications &#187; UTF-8</title>
	<atom:link href="http://blog.hitsuji.me/tag/utf-8/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hitsuji.me</link>
	<description>Webアプリつくるの楽しい！つくるネタ、使うネタ、あとは雑談です。</description>
	<lastBuildDate>Sun, 25 Jan 2015 12:49:45 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>区名と行政団体コードを書き並べたCSVからJSONファイルを作成する</title>
		<link>http://blog.hitsuji.me/convert-csv-listing-words-name-and-their-codes-to-json/</link>
		<comments>http://blog.hitsuji.me/convert-csv-listing-words-name-and-their-codes-to-json/#comments</comments>
		<pubDate>Sat, 14 Jun 2014 12:33:56 +0000</pubDate>
		<dc:creator><![CDATA[kapibarawebmaster1515]]></dc:creator>
				<category><![CDATA[可視化]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://blog.hitsuji.me/?p=94</guid>
		<description><![CDATA[こんばんは。今日は、横浜市の区名と&#8221;id&#8221;+行政団体コードをリストアップしたCSVファイルから、JSONファイルにコンバートします。これを後に、TopoJSONファイルと一緒に・・・<a class="readon" href="http://blog.hitsuji.me/convert-csv-listing-words-name-and-their-codes-to-json/">続きを読む</a>]]></description>
				<content:encoded><![CDATA[<p>こんばんは。今日は、横浜市の区名と&#8221;id&#8221;+行政団体コードをリストアップしたCSVファイルから、JSONファイルにコンバートします。これを後に、TopoJSONファイルと一緒に読み込んで、横浜市地図のラベル表示に使う訳です。（地図表示については、<a title="横浜市地図の各区に１つだけラベル表示する" href="http://blog.hitsuji.me/show-yokohama-city-map-with-label-of-unique-id/">前回までの記事</a>を読んでね）</p>
<p>&nbsp;</p>
<h4>CSVファイルを用意する</h4>
<p>区名と団体コードをリストアップしたCSVファイルをUTF-8エンコードで作成します。ちなみに、もしあなたがMac使いなら、NumbersでUTF-8でさくっと出力することができます。Excelだとshift-jisが基本なので、適当なエディタでUTF-8に変換してくださいねー</p>
<h6>yokohama_codes.csv</h6>
<script src="https://gist.github.com/41f4f657f8fb67184a44.js"></script><noscript><pre><code class="language-csv csv">cityid,name
id141011,鶴見区
id141020,神奈川区
id141038,西区
id141046,中区
id141054,南区
id141062,保土ヶ谷区
id141071,磯子区
id141089,金沢区
id141097,港北区
id141101,戸塚区
id141119,港南区
id141127,旭区
id141135,緑区
id141143,瀬谷区
id141151,栄区
id141160,泉区
id141178,青葉区
id141186,都筑区</code></pre></noscript>
<p>&nbsp;</p>
<h4>PythonでCSVからJSONに変換する</h4>
<p>１行目はカラム名と見なして要素名とし、行毎にDictionaryを作成、Listにポンポン入れて最後にJSON形式で書き出します。</p>
<h6>conv_csv_to_json.py</h6>
<script src="https://gist.github.com/82743ce5eecbf1b02c69.js"></script><noscript><pre><code class="language-python python">#coding:utf-8
'''
Created on 2014/03/27

@author: Tae Matsumoto
CSVファイルを読み込み、同名のJSONファイルを出力します。
'''

import csv, json

filename = 'yokohama_codes'

header = []
data = []

def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
    # csv.py doesn't do Unicode; encode temporarily as UTF-8:
    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),dialect=dialect, **kwargs)
    for row in csv_reader:
        # decode UTF-8 back to Unicode, cell by cell:
        yield [unicode(cell, 'utf-8') for cell in row]

def utf_8_encoder(unicode_csv_data):
    for line in unicode_csv_data:
        yield line.encode('utf-8')

with open(filename + '.csv', 'rU') as csvfile:
    spamreader = unicode_csv_reader(csvfile, dialect=csv.excel)
    is_first = True
    
    for row in spamreader:
        if is_first:
            header = row[:]
            is_first = False
            continue
        items = {}
        for i in range(0, len(row)):
            item = row[i]
            items[header[i]] = item
        data.append(items)

with open(filename+'.json', 'w') as f:
    json.dump(data, f, ensure_ascii=False, indent=2, encoding='utf8')
    f.close()
</code></pre></noscript>
<p>&nbsp;</p>
<p>JSONファイルに日本語が含まれるので、json.dumpにensure_ascii=falseとencoding=&#8217;utf8&#8217;を指定します。</p>
<p><a title="saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence" href="http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence" target="_blank">http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence</a></p>
<p>&nbsp;</p>
<p>上手くできました♥︎</p>
<p>次回は、今日作成したJSONファイルと前回作成した地図をMixして、横浜市地図に区名ラベルを表示できるようにしたいと思います。</p>
<p>それでは、また♪</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hitsuji.me/convert-csv-listing-words-name-and-their-codes-to-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
