跳转到主要内容

智能文本截断。

项目描述

智能文本截断。

https://travis-ci.cn/icemac/icemac.truncatetext.svg?branch=master https://coveralls.io/repos/github/icemac/icemac.truncatetext/badge.svg?branch=travis Current version on PyPI Supported Python versions

Fork我: https://github.com/icemac/icemac.truncatetext

优雅地截断文本

智能文本截断意味着截断不会在单词内部发生,而是在单词之间。因此所需长度只是一个近似值。结果文本可能比所需长度略长

>>> from icemac.truncatetext import truncate
>>> 'I was here.'[:3]
'I w'
>>> truncate('I was here.', 3)
'I was ...'

什么可以被截断?

只有basestring(Python 3中的str)实例可以被截断

>>> truncate(3, 3)
Traceback (most recent call last):
ValueError: 3 is no instance of basestring or None
>>> print(truncate(u'Lorem ipsum', 5))
Lorem ...

None处理得很好

>>> truncate(None, 4)
''

返回什么?

即使返回的文本比所需长度长,也会至少返回一个单词

>>> truncate('Lorem ipsum', 1)
'Lorem ...'

如果文本只包含一个比期望长度长的单词,它将被返回,而不带省略号

>>> truncate('The-really-long-word', 5)
'The-really-long-word'

如果文本比期望长度短,它也将不带省略号返回

>>> truncate('Lorem ipsum', 11)
'Lorem ipsum'

文本在哪里被截断?

截断也会在制表符和换行符处发生

>>> truncate("I was here.\nNow I'm away", 11)
'I was here. ...'
>>> truncate("I was here.\rNow I'm away", 12)
'I was here.\rNow ...'
>>> truncate("I was here.\tNow I'm away", 11)
'I was here. ...'

我不希望以省略号作为结尾。

truncate 函数接受一个可选参数,用于定义省略号字符串

>>> print(truncate(u'Lorem ipsum', 5, ellipsis=u':::'))
Lorem :::

变更日志

1.1.1 (2018-11-16)

  • 将仓库迁移到 Github。

1.1 (2018-11-15)

  • 添加对 Python 3.7 的支持。

1.0.post1 (2017-06-22)

  • 修复 描述,以便 PyPI 可以接受它。

1.0 (2017-05-13)

  • 添加对 Python 3.4 至 3.6、PyPy2 和 PyPy3 的支持。

  • 停止对 Python 2.5 和 2.6 的支持。

  • 将许可证从 ZPL 更改为 MIT。

0.3 (2012-11-13)

  • 已将源代码迁移到 bitbucket.org。

  • 添加 Travis-CI。

  • 停止对 Python 2.3 和 Python 2.4 的官方支持。(尽管这些相当旧的 Python 版本可能仍然可以与这个版本一起使用。)

0.2.1 (2011-07-28)

  • 更新类别:也支持 Python 2.7。

0.2 (2009-09-26)

  • 添加了传递用作省略号字符串的字符串的能力。

0.1 (2009-01-31)

  • 首次公开发布。

黑客攻击

bitbucket.org 上 fork 我或向 问题跟踪器 报告问题。

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

icemac.truncatetext-1.1.1.tar.gz (5.3 kB 查看散列)

上传时间

构建分发

icemac.truncatetext-1.1.1-py2.py3-none-any.whl (7.8 kB 查看散列)

上传时间 Python 2 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面