Created
February 12, 2019 15:05
-
-
Save harhogefoo/db69351789dfa2f60e9c5832eef24b13 to your computer and use it in GitHub Desktop.
全角文字を半角文字2つとしてカウントする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unicodedata import east_asian_width | |
def count_text(message): | |
# 全角文字を半角文字2つとしてカウントする | |
width_dict = { | |
'F': 2, # Fullwidth | |
'H': 1, # Halfwidth | |
'W': 2, # Wide | |
'Na': 1, # Narrow | |
'A': 2, # Ambiguous | |
'N': 1 # Neutral | |
} | |
chars = [char for char in message] | |
east_asian_width_list = [east_asian_width(char) for char in chars] | |
width_list = [width_dict[east_asian_width] | |
for east_asian_width in east_asian_width_list] | |
return sum(width_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment