Skip to content

Instantly share code, notes, and snippets.

@harhogefoo
Created February 12, 2019 15:05
Show Gist options
  • Save harhogefoo/db69351789dfa2f60e9c5832eef24b13 to your computer and use it in GitHub Desktop.
Save harhogefoo/db69351789dfa2f60e9c5832eef24b13 to your computer and use it in GitHub Desktop.
全角文字を半角文字2つとしてカウントする
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