我希望变量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
脚本源代码:
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}\"" % fname,
pp(output_format)
cmd =[
"vlc",
"-I dummy",
"--rtsp-tcp {0}".format(self.conn_cfg["rtsp_link"]),
output_format,
"--stop-time {0} vlc://quit ".format(period)
]
cmd_str = " ".join(cmd)
self.run(cmd_str)
答案 0 :(得分:12)
您的output_format
始终是元组,因为您在每个可能的值之后加上逗号:
output_format = "..." % fname,
# ---------------------------^
删除这些逗号,cmd_str
将再次只包含字符串。
Python元组由这样的逗号组成;只有在不使用它们时才需要括号会导致含糊不清:
>>> 1
1
>>> 1,
(1,)
>>> type(_)
<class 'tuple'>