Truncate a string without ending in the middle of a word

后端 未结 8 1835
无人共我
无人共我 2020-12-12 17:17

I am looking for a way to truncate a string in Python that will not cut off the string in the middle of a word.

For example:

Original:          \"This is          


        
8条回答
  •  渐次进展
    2020-12-12 17:39

    def smart_truncate1(text, max_length=100, suffix='...'):
        """Returns a string of at most `max_length` characters, cutting
        only at word-boundaries. If the string was truncated, `suffix`
        will be appended.
        """
    
        if len(text) > max_length:
            pattern = r'^(.{0,%d}\S)\s.*' % (max_length-len(suffix)-1)
            return re.sub(pattern, r'\1' + suffix, text)
        else:
            return text
    

    OR

    def smart_truncate2(text, min_length=100, suffix='...'):
        """If the `text` is more than `min_length` characters long,
        it will be cut at the next word-boundary and `suffix`will
        be appended.
        """
    
        pattern = r'^(.{%d,}?\S)\s.*' % (min_length-1)
        return re.sub(pattern, r'\1' + suffix, text)
    

    OR

    def smart_truncate3(text, length=100, suffix='...'):
        """Truncates `text`, on a word boundary, as close to
        the target length it can come.
        """
    
        slen = len(suffix)
        pattern = r'^(.{0,%d}\S)\s+\S+' % (length-slen-1)
        if len(text) > length:
            match = re.match(pattern, text)
            if match:
                length0 = match.end(0)
                length1 = match.end(1)
                if abs(length0+slen-length) < abs(length1+slen-length):
                    return match.group(0) + suffix
                else:
                    return match.group(1) + suffix
        return text
    

提交回复
热议问题