Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Created December 16, 2011 07:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcayzac/1485005 to your computer and use it in GitHub Desktop.
Save jcayzac/1485005 to your computer and use it in GitHub Desktop.
Python: Convert all ASCII characters to their full-width counterpart
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
WIDE_MAP = dict((i, i + 0xFEE0) for i in xrange(0x21, 0x7F))
WIDE_MAP[0x20] = 0x3000
def widen(s):
"""
Convert all ASCII characters to the full-width counterpart.
>>> print widen('test, Foo!')
test, Foo!
>>>
"""
return unicode(s).translate(WIDE_MAP)
while True:
line = sys.stdin.readline()
if not line: break
sys.stdout.write(widen(line.decode('utf-8')).encode('utf-8'))
@asdkant
Copy link

asdkant commented Jan 9, 2021

I've ported it to python 3:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, fileinput

WIDE_MAP = {i: i + 0xFEE0 for i in range(0x21, 0x7F)}
WIDE_MAP[0x20] = 0x3000

def widen(s):
    """
    Convert all ASCII characters to their full-width counterpart.
    
    >>> print widen('test, Foo!')
    test, Foo!
    >>> 
    """
    return s.translate(WIDE_MAP)

for line in fileinput.input():
    sys.stdout.write(widen(line))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment