顧客管理で住所の入力は出来るだけカンタンにしたい。

ということで、郵便番号辞書は必須。

郵便番号を読み込むプログラムを試してみた。
始めは自分で作ってたんだけど、読み込みに時間がかかるのでまたもやネットを探してみた。

TSUYUKI.MAKOTO にあった記事を元に、ワントランザクションでのプログラムにしてみた。

データモデル

from django.db import models

# Create your models here.

class ZipCode(models.Model):
zipcode = models.CharField(‘Zip Code’, max_length=7, db_index=True)
prefecture_kana = models.CharField(‘Prefecture Kana’, max_length=20)
city_kana = models.CharField(‘City Kana’, max_length=100)
town_kana = models.CharField(‘Town Kana’, max_length=100)
prefecture = models.CharField(‘Prefecture’, max_length=20)
city = models.CharField(‘City’, max_length=100)
town = models.CharField(‘Town’, max_length=100)

def __unicode__(self):
return ‘%s: %s %s %s’ % (self.zipcode, self.prefecture, self.city, self.town)

読み込みようプログラム

import sys, os, csv, time
from django.core.management import setup_environ
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write(“Error: Can’t find the file ‘settings.py’ in the directory containing %r. It appears you’ve customized things.\nYou’ll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it’s causing an ImportError somehow.)\n” % __file__)
sys.exit(1)

setup_environ(settings)

from django.conf import settings
from django.db import IntegrityError, transaction
from zipcode.models import ZipCode

try:
import zenhan
except ImportError:
print ”’You need zenhan.py .
Download zenhan.py and install it.

http://matatabi.homeip.net/blog/setomits/877

”’
sys.exit()

#
#from django.db import connection
#cursor = connection.cursor()
#cursor.execute(‘PRAGMA temp_store = MEMORY;’)
#cursor.execute(‘PRAGMA synchronous=OFF’)

from django.core.management import call_command
call_command(‘syncdb’)

reader = csv.reader(open(‘KEN_ALL.CSV’, ‘rb’))

@transaction.commit_manually
def import_zip(reader):
counter = 0
start = time.clock()
for data in reader:
try:
zipcode = ZipCode(zipcode=data[2],
prefecture_kana=zenhan.h2z(unicode(data[3], ‘shift_jis’), zenhan.KANA),
city_kana=zenhan.h2z(unicode(data[4], ‘shift_jis’), zenhan.KANA),
town_kana=zenhan.h2z(unicode(data[5], ‘shift_jis’), zenhan.KANA),
prefecture=unicode(data[6], ‘shift_jis’),
city=unicode(data[7], ‘shift_jis’),
town=unicode(data[8], ‘shift_jis’))
zipcode.save()
except IntegrityError: pass
counter += 1
if counter%1000 == 0:
print ‘%d: %.2f’ % (counter, time.clock() – start)

transaction.commit()
print ‘end: %.2f’ % (time.clock() – start)

import_zip(reader)

トランザクションをマニュアルにするには

from django.db import transaction

でdjango.dbのトランザクションをロードしておいて
@transaction.comit_manually
def トランザクションをマニュアル処理する関数の定義

としなければならない。
@transaction.commit_manually の意味が始めはよく分からなくて(後ろに続く関数が引数になっていると分かっていなくて)、SyntaxErrorが出ていたのでかなり悩んだ。

ワントランザクションにすると、延々と終わらなかった約12万件の郵便番号データが 59.51秒(約1分)で読み込み完了した。

データベースはwindowsXP上のsqlite3.6.16

<追記>

ソースコードがインデントされていない・・・orz

Python はインデントで構造を作っているので、これは問題だなー。

ソースコードをWebに載せる方法を考えなくては。

とりあえず、このソースコードはインデントされていないので、注意


関連する記事:

  1. 郵便番号辞書をDjangoアプリに取り込む2
  2. csvファイルからDjango用のFixtureデータ(JSON形式)を作るツール
  3. CodeZineのDjangoチュートリアルで7つの詰まったところ
  4. Djangoの汎用ビューでつまった
  5. Djangoのモデルにフィールドを追加する方法
  6. Django(ジャンゴ)のドキュメントは分かりやすい
  7. WordPressのバックアップ
  8. 個人情報のダミーデータを作ってくれるサイト