2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで和暦西暦変換を7行で書く

Last updated at Posted at 2022-10-02

和暦から西暦への変換。最も簡単なコードが欲しかったのでいろいろ探すのが面倒で自分で作った。

wareki2seireki.py
import re
def wareki2seireki(wareki_s, def_value=0):
    era_dic = {"明治": 1868, "大正": 1912, "昭和": 1926, "平成": 1989, "令和": 2019}
    s = re.match(r'(明治|大正|昭和|平成|令和)([0-9]+|元)年', str(wareki_s))
    if s is None: return def_value
    y = int(s.group(2)) if s.group(2) != '' else 1
    return era_dic[s.group(1)] + y - 1

print(wareki2seireki('昭和60年'))
print(wareki2seireki('令和元年'))
2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?