跳转到主要内容

Python文本操作模块

项目描述

python-textops提供了许多在字符串级别、列表级别或整个文本级别的文本操作。
这些操作可以用点号或管道符号进行链式操作。
链式操作存储在一个单独的懒加载对象中,只有在提供输入文本时才会执行。

安装

要安装

pip install python-textops

概览

使用textops的通常方式如下。重要:请注意,textops库重新定义了Python的位或运算符‘|’,以便在Unix shell中用作管道

from textops import *

result = "an input text" | my().chained().operations()

or

for result_item in "an input text" | my().chained().operations():
   do_something(result_item)

or

myops = my().chained().operations()
# and later in the code, use them :
result = myops("an input text")
or
result = "an input text" | myops

“输入文本”可以是

  • 一个简单的字符串,

  • 一个多行字符串(一个包含换行符的字符串),

  • 一个字符串列表,

  • 一个字符串生成器,

  • 一个列表的列表(当您需要将行切割成列时很有用),

  • 一个字典列表(当您解析一行时很有用)。

因此,可以进行以下操作

>>> 'line1line2line3' | grep('2').tolist()
['line1line2line3']
>>> 'line1\nline2\nline3' | grep('2').tolist()
['line2']
>>> ['line1','line2','line3'] | grep('2').tolist()
['line2']
>>> [['line','1'],['line','2'],['line','3']] | grep('2').tolist()
[['line', '2']]
>>> [{'line':1},{'line':'2'},{'line':3}] | grep('2').tolist()
[{'line': '2'}]

示例

管道和点号表示法(推荐)

>>> print 'this is an error\nthis is a warning' | grepi('error').first().upper()
THIS IS AN ERROR

您可以在任何地方使用管道(内部略微不太优化,但看起来像shell)

>>> print 'this is an error\nthis is a warning' | grepi('error') | first() | strop.upper()
THIS IS AN ERROR

要从字符串、列表或字典中直接执行操作(使用点号表示法),您必须使用textops扩展类型:StrExtListExtDictExt

>>> s = StrExt('this is an error\nthis is a warning')
>>> print s.grepi('error').first().upper()
THIS IS AN ERROR

文档

请在此处阅读文档:http://python-textops.readthedocs.org

新闻

0.3.6 (2019-010-01)

  • 向parse_smart()添加分隔符参数

0.3.5 (2019-09-25)

  • 向parse_smart()添加key_filter参数

0.3.4 (2018-05-25)

  • 添加since()和until()

0.3.3 (2018-02-23)

  • 修复get_attribute_or_textop()

0.3.2 (2017-06-18)

  • DebugText类修复

0.3.1 (2017-04-05)

  • 修复了当管道输入字符串为空字符串时的错误

0.3.0 (2017-03-23)

  • 添加了文件操作和运行操作

0.2.14 (2017-02-28)

  • sed() 和 sedi() 现在接受搜索模式和替换字符串的列表

0.2.12 (2017-02-27)

  • 添加了 tofile() 和 teefile()

0.2.11 (2017-01-31)

  • 在 setup.py 中移除了对 sphinx 包的版本限制

0.2.10 (2017-01-26)

  • 添加了 textops.extend_type

0.2.9 (2016-12-06)

  • 修复了在 state_pattern() 中没有 groupdict 时的 autostrip

  • 在 state_pattern() 中添加了 __continue__ 以跳转到 _state

  • parsek* 和 keyval 现在可以解析字符串列表

0.2.8 (2016-11-02)

  • 修复了 MySQLdb 不支持 UnicodeExt 的问题

  • 以 '_' 开头的可调用属性将不再扩展

0.2.6 (2016-04-12)

  • 改进了 state_pattern() 操作中的输出数据路径

0.2.5 (2016-04-12)

  • 添加了 sgrep*() 操作

0.2.4 (2016-03-23)

  • 添加了 dostrip() 操作

  • 改进了 sed() 操作

0.2.3 (2016-03-17)

  • 添加了 wcount*() 操作

0.2.2 (2016-02-16)

  • cut 和 cutre 现在接受 maxsplit 参数

0.2.1 (2016-02-16)

  • 添加了 aggregate() 列表操作

0.2.0 (2015-12-16)

  • 在 ipython 中改进了 repr 显示

0.1.9 (2015-12-15)

  • 添加了 eformat

  • 为格式化和渲染操作添加了上下文字典参数

0.1.8 (2015-12-10)

  • 添加了 less() 和 skess() 列表操作

  • 添加了 parse_smart() 解析器

0.1.7 (2015-11-26)

  • 添加了一些操作

  • 性能调整

0.1.3 (2015-11-20)

  • 调整了许多事情

  • 所有内容都已文档化

0.1.2 (2015-11-04)

  • 更多文档和 doctests

0.1.1 (2015-11-04)

第一个工作包

项目详情


下载文件

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

源分发

python-textops-0.3.6.tar.gz (76.8 kB 查看散列值)

上传时间

构建分发

python_textops-0.3.6-py2-none-any.whl (55.0 kB 查看散列值)

上传时间 Python 2

支持者

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