“raise”导致语法错误

2024-03-29 09:46:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个剧本:

#!/usr/bin/python
import sys
import ast

def main(argv):
  for line in sys.stdin:
    try:
      rules, scores = line.split('\t')
      scores = ast.literal_eval(scores)
      print '\t'.join([rules, str(any(scores))])
    except:
      sys.stderr.write('Got line ' + line)
      raise

if __name__ == "__main__":
  main(sys.argv[0]) # 0 as there are no args besides the hive query fields

对于预期的输入,脚本按预期工作

$ echo -e "[{2,3},{4,3}]\t[1]" | ./check_anom.py
[{2,3},{4,3}]   True

但当输入出错时:

$ echo -e "[{2,3},{4,3}]\t" | ./check_anom.py
Got line [{2,3},{4,3}]
Traceback (most recent call last):
  File "./check_anom.py", line 19, in <module>
    main(sys.argv[0]) # 0 as there are no args besides the hive query fields
  File "./check_anom.py", line 12, in main
    scores = ast.literal_eval(scores)
  File "/usr/lib/python2.7/ast.py", line 49, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python2.7/ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1

    ^
SyntaxError: unexpected EOF while parsing

我本以为会有一个ValueError,为什么我会得到SyntaxError?你知道吗


Tags: inpyimportmainusrcheckevalsys
1条回答
网友
1楼 · 发布于 2024-03-29 09:46:00

看起来你想eval(""),因为你的[1]在你split\t后面丢失了。这是在你的第一个命令,但不是在一个抛出错误。你知道吗

Raise没有引起语法错误,eval("")引起语法错误,Raise只是引起语法错误。你知道吗

相关问题 更多 >