Pyton中如何进行日文的全角半角转换


安装第三方库

pip install jaconv

用法

import jaconv

# Hiragana to Katakana 平假名 ===> 片假名
jaconv.hira2kata(u'ともえまみ')
# => u'トモエマミ'

# Hiragana to half-width Katakana 平假名 ===> 半角片假名
jaconv.hira2hkata(u'ともえまみ')
# => u'トモエマミ'

# Katakana to Hiragana 片假名 ====> 平假名
jaconv.kata2hira(u'巴マミ')
# => u'巴まみ'

# half-width character to full-width character 半角 ===> 全角
jaconv.h2z(u'ティロ・フィナーレ')
# => u'ティロ・フィナーレ'

# half-width character to full-width character
# but only ascii characters 只限ascii字符
jaconv.h2z(u'abc', ascii=True)
# => u'abc'

# half-width character to full-width character
# but only digit characters
jaconv.h2z(u'123', digit=True)
# => u'123'

# half-width character to full-width character
# except half-width Katakana 除半角片假名
jaconv.h2z(u'アabc123', kana=False, digit=True, ascii=True)
# => u'アabc123'

# full-width character to half-width character 全角 ===> 半角
jaconv.z2h(u'ティロ・フィナーレ')
# => u'ティロ・フィナーレ'

# full-width character to half-width character
# but only ascii characters 只限ascii字符
jaconv.z2h(u'abc', ascii=True)
# => u'abc'

# full-width character to half-width character
# but only digit characters 只限数字
jaconv.z2h(u'123', digit=True)
# => u'123'

# full-width character to half-width character
# except full-width Katakana 除全角片假名
jaconv.z2h(u'アabc123', kana=False, digit=True, ascii=True)
# => u'アabc123'

# normalize
jaconv.normalize(u'ティロ・フィナ〜レ', 'NFKC')
# => u'ティロ・フィナーレ'

# Hiragana to alphabet 平假名===>罗马字
jaconv.kana2alphabet(u'じゃぱん')
# => japan

# Alphabet to Hiragana 罗马字===>平假名
jaconv.alphabet2kana(u'japan')
# => じゃぱん