跳转到主要内容

Python文本操作模块

项目描述

python-textops3提供许多在字符串级别、列表级别或全文级别的文本操作。
这些操作可以使用“点”或“管道”表示法进行链式操作。
链式操作存储在单个惰性对象中,只有在提供输入文本时才会执行。

安装

要安装

pip install python-textops3

概述

使用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-textops3.readthedocs.org

新闻

3.2.1 (2022-03-31)

  • 添加Python 3.10支持

3.1.0 (2020-06-25)

  • 添加TupleExt

  • DictExt现在支持pickle

3.0.8 (2019-10-08)

  • 向cat()添加encoding & encoding_errors参数

3.0.7 (2019-10-01)

  • 向parse_smart()添加separators参数

3.0.6 (2019-09-25)

  • 将 key_filter 参数添加到 parse_smart() 函数中

3.0.5 (2019-06-14)

  • 添加 decode_bytes() 函数

3.0.3 (2018-11-07)

  • 从 Python 2 迁移到 Python 3

项目详情


下载文件

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

源分发

python-textops3-3.2.1.tar.gz (89.9 kB 查看哈希值)

上传时间

构建分发

python_textops3-3.2.1-py3-none-any.whl (62.2 kB 查看哈希值)

上传时间 Python 3