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扩展类型:StrExt,ListExt或DictExt:
>>> s = StrExt('this is an error\nthis is a warning') >>> print(s.grepi('error').first().upper()) THIS IS AN ERROR
3.0.5(2019-06-14)
- add decode_bytes()
3.0.3(2018-11-07)
- 从python 2迁移到python 3