Are there anything similar to “perl -pe” option in

2019-06-26 13:06发布

TL;DR I want to know what is an option in Python, which might roughly correspond to the "-pe" option in Perl.

I used to use PERL for some time, but these days, I have been switching to PYTHON for its easiness and simplicity. When I needed to do simple text processing on the shell prompt, I had used perl -pe option. The following is an example.

grep foo *.txt | perl -pe 's/foo/bar/g'

To do a similar thing in Python, could someone explain to me how I can do this?

标签: python shell
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-26 13:43

Perl, although a fully fledged as programming language, was initially thought, and evolved as, a tool for text manipulation.

Python, on th other hand, has always been a general purpose programing language. It can handle text and text flexibility, with enormous flexibility, when compared with, say Java or C++, but it will do so within its general syntax, without exceptions to those rules, including special syntax for regular expressions, that in absence of other directives become a program in themselves.The same goes for opening, reading and writting files, given their names.

So, you can do that with "python -c ..." to run a Python script from the command line - but your command must be a full program - with beginning, middle and end.

So, for a simple regular expression substitution in several files passed in the stdin, you could try something along:

grep foo *txt| python3 -c "import sys, re, os; [(open(filename + 'new', 'wt').write(re.sub ('foo', 'bar', open(filename).read())), os.rename(filename + "new", filename))for filename in sys.stdin]"
查看更多
Bombasti
3楼-- · 2019-06-26 13:48

-pe is a combination of two arguments, -p and -e. The -e option is roughly equivalent to Python's -c option, which lets you specify code to run on the command line. There is no Python equivalent of -p, which effectively adds code to run your passed code in a loop that reads from standard input. To get that, you'll actually have to write the corresponding loop in your code, along with the code that reads from standard input.

查看更多
登录 后发表回答