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

如果将-h传递给命令行,则会尝试使用该名称打开文件.

拆分命令行解析处理

您的预处理函数不关心命令行参数,它应该将打开的文件描述符作为参数.

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

使用docopt的示例解决方案

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

首先安装docopt:

$pip install docopt

这是fromstdin.py代码:

"""fromstdin - Training and Testing Framework

Usage: fromstdin.py [options]

Options:

--text= Text model [default: text.txt]

--features= Features model [default: features.txt]

--test= Testing set [default: testset.txt]

--vectorizer= The vectorizec [default: vector.txt]

Read data from 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("

")

print "processing done"

if __name__ == "__main__":

from docopt import docopt

args = docopt(__doc__)

print args

infile = args[""]

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]

显示帮助:

$python fromstdin.py -h

fromstdin - Training and Testing Framework

Usage: fromstdin.py [options]

Options:

--text= Text model [default: text.txt]

--features= Features model [default: features.txt]

--test= Testing set [default: testset.txt]

--vectorizer= The vectorizec [default: vector.txt]

Read data from file. Use "-" for reading from stdin.

使用它,从stdin喂养:

(so)javl@zen:~/sandbox/so/cmd$ls | python fromstdin.py -

{'--features': 'features.txt',

'--test': 'testset.txt',

'--text': 'text.txt',

'--vectorizer': 'vector.txt',

'': '-'}

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

pythonstdin_Python从Stdin读取参数相关推荐

  1. 如何在Python中从stdin读取

    There are three ways to read data from stdin in Python. 在Python中,有三种方法可以从stdin读取数据. sys.stdinsys.std ...

  2. Linux shell 学习笔记(10)— 处理用户输入(命令行读取参数、读取用户输入、超时处理)

    1. 命令行参数 向 shell 脚本传递数据的最基本方法是使用命令行参数.命令行参数允许在运行脚本时向命令行添加数据. $ ./addem 10 30 本例向脚本 addem 传递了两个命令行参数( ...

  3. Python解析命令行读取参数 -- argparse模块

    在多个文件或者不同语言协同的项目中,python脚本经常需要从命令行直接读取参数.万能的python就自带了argprase包使得这一工作变得简单而规范.PS:optparse包是类似的功能,只不过写 ...

  4. linux写参数文件,linux下纯C++读取参数配置文件

    微软把程序猿养懒了.如果你在windows里写程序,那么本文你可以不看,有GetPrivateProfileString等来读取:如果你在qt里写程序,那么本文也可以不看.如果你在linux或者mac ...

  5. python 命令行解析模块_Python解析命令行读取参数 -- argparse模块

    在多个文件或者不同语言协同的项目中,python脚本经常需要从命令行直接读取参数.万能的python就自带了argprase包使得这一工作变得简单而规范.PS:optparse包是类似的功能,只不过写 ...

  6. 为什么在C ++中从stdin读取行比Python慢​​得多?

    我想比较使用com/thread-373690-1-1.html'>Python和C ++从stdin读取的字符串输入的行数,并震惊地看到我的C ++代码运行速度比等效的Python代码慢一个数 ...

  7. java从配置文件中读取参数

    java从配置文件中读取参数的方式 有的时候我们需要将一些固定的配置信息写到yml文件中去,然后在类中在读取 #首先就得先在yml文件中配置参数 upload:param:"test&quo ...

  8. 分区 读取参数错误_如何修复“警告:重新读取分区表失败,错误22:参数无效”在Linux上错误...

    分区 读取参数错误 If you are getting the following error while trying to change the default fat32 partition ...

  9. C++:从json文件中读取参数/创建文件夹/位拼接

    目录 1.从json文件中读取参数 2.创建文件夹 3.位运算拼接 1.从json文件中读取参数 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml ...

最新文章

  1. 《OpenStack实战》——第1章 介绍OpenStack 1.1OpenStack是什么
  2. python使用numpy中的np.mean函数计算数组的均值、np.var函数计算数据的方差、np.std函数计算数组的标准差
  3. c++中使用Tesseract-OCR
  4. openLDAP的编译安装以及配置
  5. 将STM32的标准库编译成lib
  6. Android之如何用dextra.ELF64查看安卓手机“设置“图标的源代码
  7. 微信小程序视频弹幕效果
  8. mysql begin_MySQL 存储过程的 BEGIN 和 END
  9. 21-python-time,random模块的应用
  10. 前端和后端哪个工资高?
  11. 量子卡塔教您如何在Q#中进行量子编程
  12. 理论计算机科学方向,计算机科学与技术专业考研方向:计算机软件与理论
  13. 充电系统(交流慢充,直流快充)
  14. excel 怎么自动往下生成编号
  15. kotlin 如何输入箭头符号->
  16. 2.Echart----绘制饼图
  17. GIS中的拓扑空间关系
  18. oracle万能分页代码,Oracle万能分页法的存储过程
  19. 【Hive】增添字段并改变字段位置
  20. 想开发手机APP软件,首先要弄清楚以下10点

热门文章

  1. 【Hololens】微软Hololens虚拟现实视频集
  2. c oracle 分页工具类,Util工具类 跨Oracle、MySQL通用分页查询
  3. ftp上传乱码_ftp上传与wordpres常规基本设置
  4. 对抗微软?索尼36亿美金收购游戏开发商Bungie
  5. 国庆档43.87亿收官:《长津湖》一家独大 影视股喜忧参半
  6. 旷视科技IPO过会,AI技术“立业”难言轻松
  7. 这届年轻人,连泡面也买不起了
  8. 三星电子第二季芯片需求大增 但手机销售疲软
  9. 从教科书式的失败到手术刀式的自救,李宁找回了“李宁”!
  10. 100款违法违规APP下架整改:微店、更美等在列