try:
if content_type == "audio/x-wav" and len(payload):
ref_filename = get_filename('pcm', output_filename + '_ref', LNG)
if not os.path.exists(ref_filename):
with open(ref_filename, 'wb') as f_pcm:
f_pcm.write(payload)
cur_filename = get_filename('pcm', output_filename, LNG)
with open(cur_filename, 'wb') as f_pcm:
f_pcm.write(payload)
else:
print "[ERROR] No Resposne From the Server"
sys.exit(Error_Status)
compare_files(ref_filename, cur_filename)
elif content_type == "application/json":
with open(get_filename('json', output_filename, LNG), 'w') asf_json:
f_json.write(payload)
except:
print "\n[ERROR] Can't Create Reference PCM or Response JSON files!"
Error_Status = 1
如果上面代码中的IF循环没有执行,我正在尝试打印错误消息。所以我使用if循环然后在其他循环打印错误消息。后来我在比较文件。然后我正在做elif声明。我正在尝试所有这些事情,除了块。但我收到的错误是
elif content_type == "application/json":
^
SyntaxError: invalid syntax
答案 0 :(得分:0)
语法错误是因为elif
子句必须在 else
子句之前发生。以下是正确方法的概述:
if condition_1:
print('condition_1')
elif condition_2:
print('condition_2')
...
elif condition_n:
print('condition_n')
else:
print('no condition satisfied')
更正代码涉及将elif
子句及其正文移到else
子句之前:
try:
if content_type == "audio/x-wav" and len(payload):
ref_filename = get_filename('pcm', output_filename + '_ref', LNG)
if not os.path.exists(ref_filename):
with open(ref_filename, 'wb') as f_pcm:
f_pcm.write(payload)
cur_filename = get_filename('pcm', output_filename, LNG)
with open(cur_filename, 'wb') as f_pcm:
f_pcm.write(payload)
elif content_type == "application/json":
with open(get_filename('json', output_filename, LNG), 'w') as f_json:
f_json.write(payload)
else:
print "[ERROR] No Resposne From the Server"
sys.exit(Error_Status)
compare_files(ref_filename, cur_filename)
except:
print "\n[ERROR] Can't Create Reference PCM or Response JSON files!"
Error_Status = 1
有几件事:
len(payload)
JSON?except
,你应该明确定义
应该抓住的例外情况。else
子句中,错误消息显示没有
但是,服务器的响应可能已经存在了
回应只是它有一个未处理的内容类型。