我有这个剧本:
#!/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?你知道吗
看起来你想
eval("")
,因为你的[1]
在你split
的\t
后面丢失了。这是在你的第一个命令,但不是在一个抛出错误。你知道吗Raise没有引起语法错误,
eval("")
引起语法错误,Raise只是引起语法错误。你知道吗相关问题 更多 >
编程相关推荐