python getopts选项不匹配运算符' =='

发布于 2025-02-08 03:02:05 字数 674 浏览 2 评论 0 原文

按照一些示例从他们与== 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'>

following some examples from https://www.programcreek.com/python/example/121/getopt.getopt where they make option comparisons with == i.e (option == "--json"), I am trying to enter the if statement. However option will not match despite being of type string and "--json". Can anyone see what is going wrong?


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清眉祭 2025-02-15 03:02:05

您正在使用 longopts 参数错误。在选项中使用 = 指示该选项需要参数,但您没有将 data 参数传递给 - JSON 选项,您是将其作为全局参数传递到脚本(因为您从 - JSON 选项中省略了 = )。

还要注意,您必须删除选项及其参数之间的空格。最后,您绝对不应使用裸露的

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "", ["json="])
        print(opts)
    except Exception:
        print("failed to get arguments")

    for (option, value) in opts:
        print(option)
        print(type(option))
        if str(option) == "--json":
            print("enters here")

用法:

$ python test.py --json='{"firstName":"John", "lastName":"Doe"}'  
[('--json', '{firstName:John, lastName:Doe}')]
--json       
<class 'str'>
enters here  

You are using the longopts parameter incorrectly. Using an = in the option indicates that the option takes arguments but you are not passing your data 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.

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "", ["json="])
        print(opts)
    except Exception:
        print("failed to get arguments")

    for (option, value) in opts:
        print(option)
        print(type(option))
        if str(option) == "--json":
            print("enters here")

Usage:

$ python test.py --json='{"firstName":"John", "lastName":"Doe"}'  
[('--json', '{firstName:John, lastName:Doe}')]
--json       
<class 'str'>
enters here  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文