Created
December 16, 2011 07:48
-
-
Save jcayzac/1485005 to your computer and use it in GitHub Desktop.
Python: Convert all ASCII characters to their full-width counterpart
This file contains hidden or 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
#!/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')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've ported it to python 3: