Created
January 6, 2017 06:09
-
-
Save zironycho/43e1bb0a480f2b8d2b33e939ff76ebfa to your computer and use it in GitHub Desktop.
truncated for euckr
This file contains 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
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