无效语法 - python

发布于 2024-12-09 20:17:06 字数 1661 浏览 0 评论 0原文

我有一组参数,它们的所有目标都是 'search_and'、'search_not'、'search_start'、'search_then' 和 'filename'

我必须捕获错误的代码是

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None):
        parser.error(usage)
        sys.exit()

它可能看起来像一个愚蠢的错误,有人可以告诉我发生了什么事吗?

编辑#1: 我在末尾添加了“)”以确保它正确关闭,但它仍然显示无效语法。

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

编辑#2: 这是我到目前为止所拥有的。

    import optparse from OptionParser
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-a", "--all", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
    parser.add_option("-b", "--all", type="string", dest="search_not", help="search and find the lines that contain words1 not word2")
    parser.add_option("-c", "--all", type="string", dest="search_start", help="search and find a line that starts with word1 or word2")
    parser.add_option("-d", "--all", type="string", dest="search_then", help="search and find a line that has word1 followed by word2")
    parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

代码运行后: python script.py -a hi bye

我的 if 语句语法无效。

I have a set of arguments and all of their dest is 'search_and', 'search_not', 'search_start', 'search_then', and 'filename'

The code that I have to catch the error is

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None):
        parser.error(usage)
        sys.exit()

It may seem like a silly mistake, can someone tell me what is going on?

EDIT#1:
I added the ')' at the end to make sure it closes properly but it still says invalid syntax.

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

EDIT#2:
Here is what I have so far.

    import optparse from OptionParser
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-a", "--all", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
    parser.add_option("-b", "--all", type="string", dest="search_not", help="search and find the lines that contain words1 not word2")
    parser.add_option("-c", "--all", type="string", dest="search_start", help="search and find a line that starts with word1 or word2")
    parser.add_option("-d", "--all", type="string", dest="search_then", help="search and find a line that has word1 followed by word2")
    parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

After the code is run by:
python script.py -a hi bye

I get invalid syntax with the if statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

还给你自由 2024-12-16 20:17:06

if 语句之前的行

parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

没有结束语 )。

Python 解释器通常会在下一行中抱怨此类语法错误。因此,如果在任何行中出现语法错误,建议也检查前一行

The line before the if statement

parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

does not have a closing ).

Python interpreter usually complains about such syntax errors in next line.So, if you get a syntax error at any line, it is advisable to check the preceding line as well

弃爱 2024-12-16 20:17:06

这相当于您正在尝试做的事情,并且可能稍微更具可读性

if options.filename is None and None in [options.search_and, options.search_not, options.search_start, options.search_then]:
  parser.error(usage)
  sys.exit()

This is equivalent to what you're trying to do, and may be slightly more readable

if options.filename is None and None in [options.search_and, options.search_not, options.search_start, options.search_then]:
  parser.error(usage)
  sys.exit()
太傻旳人生 2024-12-16 20:17:06

您在 : 之前错过了 )。你的编辑应该能够发现这些错误。

You missed a ) before the :. Your editor should be catching these sort of mistakes.

苍风燃霜 2024-12-16 20:17:06

如果 options.filename 为 None 且 ((options.search_and 为 None) 或 (options.search_not 为 None) 或 (options.search_start 为 None) 或 (options.search_then 为 None))

if options.filename is None and ((options.search_and is None) or (options.search_not is None) or (options.search_start is None) or (options.search_then is None))

昨迟人 2024-12-16 20:17:06

在您的原始语句中,“( options.filename is None)”您应该删除最后一个“)”,将该代码替换为:

 (options.filename is None

这应该可以修复您的语法错误。

In your original statement, "( options.filename is None)" you should remove the last ")" make replace that code with:

 (options.filename is None

That should fix your syntax error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文