python文本操作模块

python-textops3的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

“输入文本”可以是:

  • a simple string,
  • a multi-line string (one string having newlines),
  • a list of strings,
  • a strings generator,
  • a list of lists (useful when you cut lines into columns),
  • a list of dicts (useful when you parse a line).

所以我们可以这样做:

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

要直接从字符串、列表或dict执行带点符号的操作, 必须使用textops扩展类型:StrExtListExtDictExt

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

文件

请,read documentation here :

新闻

3.0.5(2019-06-14)

  • add decode_bytes()

3.0.3(2018-11-07)

  • 从python 2迁移到python 3

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何使用Java中的扫描仪读取文本文件中的特定字符?   java如果我们在hibernate中开始事务但不提交它,会发生什么?   Azure CosmosDB Java Springboot中的无服务器帐户不支持spring boot设置提供吞吐量或容器自动导航   附加到新对象的Java注释?   java如何将自定义文本视图添加到。在Kotlin中添加通知操作   java Shibboleth添加_OpenSAMLcookies,导致HTTP头大小>8k   分布式传感器数据(~40Hz)的高效Java观测器设计   java如何在while循环外声明数组,但在while循环中初始化它?   用@XmlElementRef注释的java元素没有显示在JAXB编组字符串中?   java替换二维数组的值   java如何在任务栏上创建Windows7加载栏   java如何在组件注释bean中使用会话或RequestScope bean?   java netbeans freermarker插件错误:在实现版本中请求netbeans桥的插件Lexer   java谷歌地图方向。加载失败,返回服务器错误   java当我试图递归地计算两个值之间的整数之和时,为什么结果返回一个奇怪的值?   java如何通过html文件的用户获取运行时输入,以使用Jsoup进行解析?