Python 相当于 perl -pe?
我需要从一些文本文件中挑选一些数字。我可以用 grep 选出我需要的行,但不知道如何从行中提取数字。一位同事向我展示了如何使用 perl 从 bash 执行此操作:
cat results.txt | perl -pe 's/.+(\d\.\d+)\.\n/\1 /'
但是,我通常使用 Python 编写代码,而不是 Perl。所以我的问题是,我可以以同样的方式使用Python吗?即,我可以将某些内容从 bash 传输到 Python,然后将结果直接输出到 stdout 吗? ……如果这有道理的话。或者 Perl 在这种情况下更方便?
I need to pick some numbers out of some text files. I can pick out the lines I need with grep, but didn't know how to extract the numbers from the lines. A colleague showed me how to do this from bash with perl:
cat results.txt | perl -pe 's/.+(\d\.\d+)\.\n/\1 /'
However, I usually code in Python, not Perl. So my question is, could I have used Python in the same way? I.e., could I have piped something from bash to Python and then gotten the result straight to stdout? ... if that makes sense. Or is Perl just more convenient in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,您可以从命令行使用 Python。
python -c
将作为 Python 代码运行
。示例:Perl 没有直接等效于
-p
选项(自动输入/输出逐行处理),但这主要是因为 Python 不使用相同的概念$_
以及 Perl 所做的事情 - 在 Python 中,所有输入和输出都是手动完成的(通过raw_input()
/input()
,并且打印
/print()
)。对于您的特定示例:(
显然有点笨拙。最好只编写脚本来在实际的 Python 中执行此操作。)
Yes, you can use Python from the command line.
python -c <stuff>
will run<stuff>
as Python code. Example:There isn't a direct equivalent to the
-p
option for Perl (the automatic input/output line-by-line processing), but that's mostly because Python doesn't use the same concept of$_
and whatnot that Perl does - in Python, all input and output is done manually (viaraw_input()
/input()
, andprint
/print()
).For your particular example:
(Obviously somewhat more unwieldy. It's probably better to just write the script to do it in actual Python.)
您可以使用:
You can use:
理论上可以,但是 Python 没有 Perl 那样强大的正则表达式魔力,因此生成的命令会更加笨拙,特别是如果不导入
re
就无法使用正则表达式(您可能还需要 sys.stdin 的 sys)。你同事的 Perl 单行代码的 Python 等价物大约是:
You can in theory, but Python doesn't have anywhere near as much regex magic that Perl does, so the resulting command will be much more unwieldy, especially as you can't use regular expressions without importing
re
(and you'll probably needsys
forsys.stdin
too).The Python equivalent of your colleague's Perl one-liner is approximately:
您遇到的问题可以通过多种方式解决。
我认为你应该考虑直接从 Python 使用正则表达式(perl 在你的示例中所做的事情)。正则表达式位于
re
模块中。一个例子是:(我更喜欢使用
$
而不是 '\n' 作为行结尾,因为操作系统和文件编码之间的行结尾不同)如果你想从 Python 内部调用 bash 命令,您可以使用:
其中 command 是 bash 命令。我一直使用它,因为有些操作在 bash 中比在 Python 中执行更好。
最后,如果您想使用 grep 提取数字,请使用
-o
选项,该选项仅打印匹配的部分。You have a problem which can be solved several ways.
I think you should consider using regular expression (what perl is doing in your example) directly from Python. Regular expressions are in the
re
module. An example would be:(I would prefer using
$
instead of '\n' for line endings, because line endings are different between operational systems and file encodings)If you want to call bash commands from inside Python, you could use:
Where command is the bash command. I use it all the time, because some operations are better to perform in bash than in Python.
Finally, if you want to extract the numbers with grep, use the
-o
option, which prints only the matched part.Perl(或 sed)更方便。然而,如果丑陋的话,这是可能的:
Perl (or sed) is more convenient. However it is possible, if ugly:
引用 https://stackoverflow.com/a/12259852/411282:
请参阅上面链接的解释,但这有很多作用更多
perl -p
的功能,包括对多个文件名的支持以及在未给出文件名时的stdin
。https://docs.python.org/3/library/fileinput。 html#fileinput.input
Quoting from https://stackoverflow.com/a/12259852/411282:
See the explanation linked above, but this does much more of what
perl -p
does, including support for multiple file names andstdin
when no filename is given.https://docs.python.org/3/library/fileinput.html#fileinput.input
您可以使用 python 通过使用 python -c 直接从 bash 命令行执行代码,也可以使用 sys.stdin 处理通过管道传输到 stdin 的输入,请参阅 此处。
You can use python to execute code directly from your bash command line, by using
python -c
, or you can process input piped to stdin usingsys.stdin
, see here.