Created
April 6, 2011 13:18
-
-
Save ftao/905621 to your computer and use it in GitHub Desktop.
根据新浪微博的规则截断 的 Python 代码片段
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 cn_cut(status, length): | |
l = 0 | |
end = len(status) | |
for i,c in enumerate(status): | |
if ord(c) <= 0xff: | |
l += 1 | |
else: | |
l += 2 | |
if l > length * 2: | |
end = i | |
break | |
return status[:end] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment