python getopts选项不匹配运算符' =='
按照一些示例从他们与== IE(option ==“ - json”)进行选项比较的地方,我正在尝试输入if语句。但是,尽管字符串类型和“ -json”类型,但选项将不匹配。谁能看到出了什么问题?
import sys, getopt
argv = sys.argv[1:]
def main(argv):
try:
opts, args = getopt.getopt(argv,'',['json ='])
except:
print("failed to get arguments")
for (option, value) in opts:
print(option)
print(type(option))
if str(option) == "--json":
print ("enters here")
main(argv)
$ python test.py -json data
-json
< class'Str'>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
longopts
参数错误。在选项中使用=
指示该选项需要参数,但您没有将data
参数传递给- JSON
选项,您是将其作为全局参数传递到脚本(因为您从- JSON
选项中省略了=
)。还要注意,您必须删除选项及其参数之间的空格。最后,您绝对不应使用裸露的。
用法:
You are using the
longopts
parameter incorrectly. Using an=
in the option indicates that the option takes arguments but you are not passing yourdata
argument to the--json
option, you are passing it to the script as a global argument (because you omitted the=
from the--json
option).Note also that you must remove the whitespace between the option and its argument. Finally, you should never use a bare except.
Usage: