Python 从标准输入中读取参数

标签 python stdin argparse

我想从 python stdin 读取数据,但也想在我的程序中拥有输入选项。当我尝试将选项传递给我的程序时,我收到错误文件未找到并且我的参数被丢弃。

为了解析参数,我使用以下代码:

parser=argparse.ArgumentParser(description='Training and Testing Framework')

parser.add_argument('--text', dest='text',
                   help='The text model',required=True)
parser.add_argument('--features', dest='features',
                   help='The features model',required=True)
parser.add_argument('--test', dest='testingset',
                   help='The testing set.',required=True)
parser.add_argument('--vectorizer', dest='vectorizer',
                   help='The vectorizer.',required=True)
args = vars(parser.parse_args())

为了从标准输入读取,我使用以下代码:

for line in sys.stdin.readlines():
    print(preprocess(line,1))

命令行

echo "dsfdsF" |python ensemble.py -h
/usr/local/lib/python2.7/dist-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0.
  .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver))
Traceback (most recent call last):
  File "ensemble.py", line 38, in <module>
    from preprocess import preprocess
  File "/home/nikos/experiments/mentions/datasets/preprocess.py", line 7, in <module>
    with open(sys.argv[1], 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: '-h'

最佳答案

您的preprocess.py文件正在尝试读取表单 sys.argv[1]并将其作为文件打开。

如果您通过-h对于您的命令行,它正在尝试打开具有该名称的文件。

将命令行解析与处理分开

您的preprocess函数不应关心命令行参数,它应获取打开的文件描述符作为参数。

因此,在解析命令行参数后,您应该注意提供文件描述符,在您的情况下它将是 sys.stdin .

使用 docopt 的示例解决方案

argparse 没有任何问题,我最喜欢的解析器是 docopt我将用它来说明命令行解析、准备最终函数调用和最终函数调用的典型拆分。您也可以使用 argparse 实现相同的目的。

首先安装docopt:

$ pip install docopt

这里来了fromstdin.py代码:

"""fromstdin - Training and Testing Framework
Usage: fromstdin.py [options] <input>

Options:
    --text=<textmodel>         Text model [default: text.txt]
    --features=<features>      Features model [default: features.txt]
    --test=<testset>           Testing set [default: testset.txt]
    --vectorizer=<vectorizer>  The vectorizec [default: vector.txt]

Read data from <input> file. Use "-" for reading from stdin.
"""
import sys

def main(fname, text, features, test, vectorizer):
    if fname == "-":
        f = sys.stdin
    else:
        f = open(fname)
    process(f, text, features, test, vectorizer)
    print "main func done"

def process(f, text, features, test, vectorizer):
    print "processing"
    print "input parameters", text, features, test, vectorizer
    print "reading input stream"
    for line in f:
        print line.strip("\n")
    print "processing done"


if __name__ == "__main__":
    from docopt import docopt
    args = docopt(__doc__)
    print args
    infile = args["<input>"]
    textfile = args["--text"]
    featuresfile = args["--features"]
    testfile = args["--test"]
    vectorizer = args["--vectorizer"]
    main(infile, textfile, featuresfile, testfile, vectorizer)

可以这样调用:

$ python fromstdin.py
Usage: fromstdin.py [options] <input>

显示帮助:

$ python fromstdin.py -h
fromstdin - Training and Testing Framework
Usage: fromstdin.py [options] <input>

Options:
    --text=<textmodel>         Text model [default: text.txt]
    --features=<features>      Features model [default: features.txt]
    --test=<testset>           Testing set [default: testset.txt]
    --vectorizer=<vectorizer>  The vectorizec [default: vector.txt]

Read data from <input> file. Use "-" for reading from stdin.

使用它,从标准输入输入:

(so)javl@zen:~/sandbox/so/cmd$ ls | python fromstdin.py -
{'--features': 'features.txt',
 '--test': 'testset.txt',
 '--text': 'text.txt',
 '--vectorizer': 'vector.txt',
 '<input>': '-'}
processing
input parameters text.txt features.txt testset.txt vector.txt
reading input stream
bcmd.py
callit.py
fromstdin.py
scrmodule.py
processing done
main func done

关于Python 从标准输入中读取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24182482/

相关文章:

Python Argparse - 如何向默认帮助消息添加文本?

python - 无法连接到具有 unicode 名称的数据库

python - 这个Python函数应该如何调用

haskell - 在单个 session 中多次可移植地打开标准输入的句柄

c - 将传输结束写入正在运行的进程的标准输入

python - 一个不需要位置参数的可选参数

python - float32 和 float64 的真正区别

python - 有效的正则表达式

python open() - 访问被拒绝

python - 为什么即使指定了参数,argparse 也会包含可选参数的默认值?