Skip to content

Instantly share code, notes, and snippets.

@hmkz
Last active December 28, 2015 20:19
Show Gist options
  • Save hmkz/7557059 to your computer and use it in GitHub Desktop.
Save hmkz/7557059 to your computer and use it in GitHub Desktop.
年月日,時分秒など日本語で書かれた日付をyyyy-MM-DD HH:mm:ssに変換する。
#!/usr/bin/env python
# -*- encoding: utf8 -*-
import re
date_jpn = "2013年11月7日 08時43分52秒"
r = re.compile("(.*)年(.*)月(.*)日 (.*)時(.*)分(.*)秒")
# 抽出した値はタプルになっているので、タプルを配列に変換
dt = list(r.search(date_jpn).groups())
# 各値が10以下の場合、頭に「0」を付与する。
dt = map((lambda val: "%02d" % int(val) if int(val) < 10 else val), dt)
print(" ".join(["-".join(dt[0:3]), ":".join(dt[3:6])]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment