Skip to content

Instantly share code, notes, and snippets.

@zironycho
Created January 6, 2017 06:09
Show Gist options
  • Save zironycho/43e1bb0a480f2b8d2b33e939ff76ebfa to your computer and use it in GitHub Desktop.
Save zironycho/43e1bb0a480f2b8d2b33e939ff76ebfa to your computer and use it in GitHub Desktop.
truncated for euckr
def truncate_euckr(str, limit):
buf = bytes()
for c in str:
tmp = c.encode('euc-kr')
if len(buf) + len(tmp) < limit:
buf += tmp
else:
break
return buf
# testing
test1 = '안녕하세요 1234 하하 asf'
test2 = '123하d하g 끝'
truncate_euckr(test1, 5).decode('euc-kr')
# result: '안녕'
truncate_euckr(test2, 4).decode('euc-kr')
# result: '123'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment