跳转到主要内容

就像sed,但是Python!

项目描述

就像sed,但是Python!

https://travis-ci.org/geowurster/pyin.svg?branch=master https://coveralls.io/repos/geowurster/pyin/badge.svg?branch=master

为什么?

有许多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,并对评估为TrueFalse的表达式进行了更智能的过滤。

开发

安装

$ 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 (8.0 kB 查看哈希值)

上传时间 源代码

由以下支持