Created
June 29, 2014 03:16
-
-
Save yubessy/3b0e0302e8afa894950b to your computer and use it in GitHub Desktop.
長い文字列を短く表示
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
# -*- coding: utf-8 -*- | |
def shorten(s, length=80): | |
u""" | |
長い文字列を"abraca ... tabra"のように短く表示 | |
""" | |
if len(s) < length: | |
return s | |
else: | |
if isinstance(s, unicode): | |
sp = u' ... ' | |
else: | |
sp = ' ... ' | |
return s[:length / 2 - 2] + sp + s[- length / 2 + 3:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment