我刚刚在python中使用管道和I/O,我从文件中使用sys.stdin.readlines()
。python中的管道和参数
你如何阅读额外的参数? 例如:
more input.txt | python program.py 200
我要存储在我的计划“200”作为变量(INT)使用。
我尝试:
number = int(sys.argv[1])
出于测试目的,我刚才输入
print number
,但我没有得到 '200'。
更好的解释:
我有两个文件, '的test.txt' 和 'test.py'
test.py
#!/usr/bin/python
import sys
line = ""
for n in [line for line in sys.stdin.readlines()]:
print n
当运行
$ more test.txt | python test.py
它会打印出来
asdfsdf 200
sldkfjs 100
kljslkd 300
我的首要目标就是打印出来与我作为参数传递的数量
我尝试行:
test.py
#!/usr/bin/python
import sys
number = int(sys.argv[1])
line = ""
for n in [line for line in sys.stdin.readlines()]:
if n.split()[1] == number:
print n
n.split() [1]应该是文件中每一行的数字是否正确? 当运行
$ more test.txt | python test.py 200
我想回的是:
asdfsdf 200
但是,相反它是空白......
外貌并适用于我。我不确定这里有什么问题。 –
@Scott Carpenter对不起,我用更好的例子更新了这个问题 –