Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2015 15:29
Show Gist options
  • Save anonymous/b225921b98ca864d0dd6 to your computer and use it in GitHub Desktop.
Save anonymous/b225921b98ca864d0dd6 to your computer and use it in GitHub Desktop.
def str_full_to_half(in_str):
"""
Adapt from http://www.pythonclub.org/python-scripts/quanjiao-banjiao
"""
out_str = []
for char in ustring:
inside_code = ord(char)
if inside_code == 0x3000:
inside_code = 0x0020 # space
else:
inside_code -= 0xfee0
if inside_code < 0x0020 or inside_code > 0x7e:
out_str.append(char)
out_str.append(chr(inside_code))
return ''.join(out_str)
# str_full_to_half(',:')
# ',:'
# str_full_to_half('、')
# ValueError
@CubatLin
Copy link

Hi there:
ustring seems not be defined, can you revised it?thanks a lot!

@CubatLin
Copy link

def str_full_to_half(in_str):
out_str = []
for char in in_str:
inside_code = ord(char)
if inside_code == 0x3000:
inside_code = 0x0020 # space
else:
inside_code -= 0xfee0
if inside_code < 0x0020 or inside_code > 0x7e:
out_str.append(char)
out_str.append(chr(inside_code))
return ''.join(out_str)

It works!

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