如何遍历命令行上传递的所有文件行? [英] How do I iterate over all lines of files passed on the command line?

查看:23
本文介绍了如何遍历命令行上传递的所有文件行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在 Perl 中执行此操作:

I usually do this in Perl:

随便.pl

while(<>) {
    #do whatever;
}

然后 cat foo.txt |随便.pl

现在,我想在 Python 中执行此操作.我试过 sys.stdin 但我不知道如何做,就像我在 Perl 中所做的那样.如何读取输入?

Now, I want to do this in Python. I tried sys.stdin but I have no idea how to do as I have done in Perl. How can I read the input?

推荐答案

试试这个:

import fileinput
for line in fileinput.input():
    process(line)

这篇关于如何遍历命令行上传递的所有文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆