Skip to main content

Python文本操作模块

项目描述

python-textops 在字符串级别、列表级别或整个文本级别提供了许多文本操作。
这些操作可以用“dotted”或“piped”表示法链接。
链式操作存储在单个惰性对象中,只有在提供输入文本时才会执行。

安装

安装:

pip install python-textops

概述

使用 textops 的常用方法如下所示。重要提示:请注意,textops 库重新定义了 python按位 OR运算符 '|' 为了将其用作 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

您可以在任何地方使用管道(内部优化程度较低,但看起来像外壳):

>>> 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

文档

在此处阅读文档:

消息

0.3.6 (2019-010-01)

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

0.3.5 (2019-09-25)

  • 将 key_filter 参数添加到 parse_smart()

0.3.4 (2018-05-25)

  • 添加自()和直到()

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)

  • 当没有 groupdict 时,修复 state_pattern() 中的 autostrip

  • 在 state_pattern() 中为 goto _state 添加 __continue__

  • parsk* 和 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)

  • 添加聚合()列表操作

0.2.0 (2015-12-16)

  • 在 ipython 中更好的 repr 显示

0.1.9 (2015-12-15)

  • 添加电子格式

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

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)

  • 更多文档和文档测试

0.1.1 (2015-11-04)

第一个工作包

项目详情


下载文件

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

源分布

python-textops-0.3.6.tar.gz (76.8 kB 查看哈希

已上传 source

内置分布

python_textops-0.3.6-py2-none-any.whl (55.0 kB 查看哈希

已上传 py2