就像sed,但是Python!
项目描述
就像sed,但是Python!
为什么?
有许多Unix工具,如sed和awk,可以从stdin或磁盘上的文件处理文本数据,但其语法可能不友好,有时直接用for循环和if语句写一个简单的脚本会更容易。本项目旨在让您进入for循环的中间,并让您编写自己的Python表达式,快速完成任务,而不必实际编写脚本、处理I/O等。
命令行界面
本项目旨在使用包含的实用程序pyin,尽管底层的pyin.core.pmap()函数可以在其他地方与非字符串对象一起使用。
Usage: pyin [OPTIONS] EXPRESSIONS...
It's like sed, but Python!
Map Python expressions across lines of text. If an expression evaluates as
'False' or 'None' then the current line is thrown away. If an expression
evaluates as 'True' then the next expression is evaluated. If a list or
dictionary is encountered it is JSON encoded. All other objects are cast
to string.
Newline characters are stripped from the end of each line before processing
and are added on write unless disabled with '--no-newline'.
This utility employs 'eval()' internally but uses a limited scope to help
prevent accidental side effects, but there are plenty of ways to get around
this so don't pass anything through pyin that you wouldn't pass through
'eval()'.
Remove lines that do not contain a specific word:
$ cat INFILE | pyin "'word' in line"
Capitalize lines containing a specific word:
$ cat INFILE | pyin "line.upper() if 'word' in line else line"
Only print every other word from lines that contain a specific word:
$ cat INFILE | pyin \
> "'word' in line" \ # Get lines with 'word' in them
> "line.split()[::2])" \ # Grab every other word
> "' '.join(line)" # Convert list from previous expr to str
Process all input text as though it was a single line to replace carriage
returns with the system newline character:
$ cat INFILE | pyin --block \
> "line.replace('\r\n', os.newline)"
For a more in-depth explanation about exactly what's going on under the
hood, see the the docstring in 'pyin.core.pmap()':
$ python -c "help('pyin.core.pmap')"
Options:
--version Show the version and exit.
-i, --infile PATH Input text file. [default: stdin]
-o, --outfile PATH Output text file. [default: stdout]
--block Operate on all input text as though it was a single
line.
--no-newline Don't ensure each line ends with a newline character.
--help Show this message and exit.
安装
通过pip
$ pip install pyin
从主分支
$ git clone https://github.com/geowurster/pyin
$ cd pyin && python setup.py install
关于py -x呢?
本项目的大部分内容都是在几乎没有了解py和没有了解py -x的情况下编写的,它们几乎具有相同的目的。这两个项目的主要区别在于,pyin需要I/O,并对评估为True或False的表达式进行了更智能的过滤。
开发
安装
$ git clone https://github.com/geowurster/pyin
$ cd pyin
$ virtualenv venv && source venv/bin/activate
$ pip install -e .\[dev\]
$ py.test tests --cov pyin --cov-report term-missing
许可证
见LICENSE.txt
项目详情
关闭
pyin-0.5.4.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7c5645e9cbe87d1bab1cebb4231d68b1cd7778199064fa20628c076addd9a6ee |
|
MD5 | d1df0348f4c433aa3430400a73f2588e |
|
BLAKE2b-256 | 23753b9a50e3cde03f46e0be430bffc1cff61e2082d161b63b980fb94ea3c6e9 |