Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
日本郵便の郵便番号ファイルに所々ある分割されたレコードを連結する
# coding: utf-8
"""
日本郵便の郵便番号ファイルに所々ある分割されたレコードを連結する
see: http://www.post.japanpost.jp/zipcode/dl/readme.html
"""
import csv
import json
zipcode = []
with open('KEN_ALL.CSV', 'r', encoding='ms932') as f:
reader = csv.reader(f)
for row in reader:
if row[13] == '1': # 廃止は飛ばす
continue
# 一番最初の扱いがキモい
if row[12] == '0' and len(zipcode) > 0:
# 連結が必要なデータ
lastelement = zipcode[-1]
if lastelement['zipcode'] == row[2]:
lastelement['townkana'] += row[5]
lastelement['town'] += row[8]
continue
# 連結不要なデータ
tmp = {
'code':row[0],
'zipcode':row[2],
'prefkana':row[3],
'citykana':row[4],
'townkana':row[5],
'pref':row[6],
'city':row[7],
'town':row[8]
}
zipcode.append(tmp)
with open('zipcode_jp.json', 'w') as f:
json.dump(zipcode, f, ensure_ascii=False, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.