是否有一个参考某处确定何时用括号括起元组是否需要?
这是最近让我感到惊讶的一个例子:
>>> d = {}
>>> d[0,] = 'potato'
>>> if 0, in d:
File "<stdin>", line 1
if 0, in d:
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 我希望变量output_format是一个字符串.但是当我运行脚本时,它给了我一个tuple类型并抛出异常.
如果我在Python解释器中运行,它给了我一个预期的字符串.
('--sout "#standard{access=file,vcodec=h264,dst=c0_s0_h264_640x480_30_vbr_500_99_40000000.mp4}"',)
'h264'
<type 'str'>
Traceback (most recent call last):
File "streaming_verification/src/streaming_verification/scripts/streaming_verification.py", line 184, in run
self.streaming.dump_file(export_fname, 5, codec_type)
File "streaming_verification/src/streaming_verification/scripts/streaming.py", line 57, in dump_file
cmd_str = " ".join(cmd)
TypeError: sequence item 3: expected string, tuple found
Run Code Online (Sandbox Code Playgroud)
脚本源代码:
def dump_file(self,
fname='',
period=10,
codec_type="h264"):
if "h264" == codec_type:
output_format = "--sout \"#standard{access=file,vcodec=h264,dst=%s.mp4}\"" % fname,
elif "mjpeg" == codec_type:
output_format = "--sout \"#standard{access=file,vcodec=mjpg ,dst=%s.avi}\"" % fname,
elif "mpeg" == codec_type :
output_format = "--sout \"#standard{access=file,vcodec=h264,dst=%s.mp4}\"" % …Run Code Online (Sandbox Code Playgroud)