I wrote it because I wanted to align it vertically with a monospaced font.
# -*- coding: utf-8 -*-
def width(s):
return sum([_width(c) for c in s.decode('utf-8')])
def _width(c):
return 2 if _isMultiByte(c) else 1
def _isMultiByte(c):
return ord(c) > 255
assert 0 == width('')
assert 1 == width('a')
assert 1 == width('A')
assert 1 == width('A')
assert 1 == width('-')
assert 1 == width('+')
assert 1 == width(' ')
assert 1 == width('\\')
assert 1 == width('\n')
assert 1 == width('\t')
assert 2 == width('aa')
assert 2 == width('AA')
assert 2 == width('But')
assert 2 == width('Tsu')
assert 2 == width('Po')
assert 2 == width('I')
assert 2 == width('!')
assert 2 == width('G')
assert 2 == width('A')
assert 2 == width('○')
assert 2 == width('■')
assert 2 == width('、')
assert 2 == width('。')
assert 2 == width('‐')
assert 2 == width('-')
assert 2 == width(' ')
assert 2 == width('\\\\')
assert 2 == width('\r\n')
assert 4 == width('YY')
assert 4 == width('0w0')
assert 4 == width('OK!')
Only A
is strange ... It is judged to be full-width ...
Correct only _isMultiByte
# -*- coding: utf-8 -*-
def width(s):
return sum([_width(c) for c in s.decode('utf-8')])
def _width(c):
return 2 if _isMultiByte(c) else 1
def _isMultiByte(c):
import unicodedata
return unicodedata.east_asian_width(c) in ['F', 'W', 'A']
This time everything is fine! Does this look good?
I'm assuming standard input
$echo Howa| python width.py
8
width.py
# -*- coding: utf-8 -*-
import sys
s = sys.stdin.readlines()[0].strip()
print width(s)
Command line arguments
$ python width.py howa
8
width.py
# -*- coding: utf-8 -*-
import sys
s = sys.argv[1]
print width(s)
Also, Greek letters and Arabic letters are not covered by the operation guarantee.
Recommended Posts