new*_*ike 4 python string tuples
我希望变量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}\"" % 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)
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 13
你output_format总是元组,因为你在每个可能的值后面加上一个逗号:
output_format = "..." % fname,
# ---------------------------^
Run Code Online (Sandbox Code Playgroud)
删除这些逗号,您的cmd_str遗嘱将再次只包含字符串.
Python元组由这样的逗号组成; 只有在不使用它们时才需要括号会导致含糊不清:
>>> 1
1
>>> 1,
(1,)
>>> type(_)
<class 'tuple'>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1894 次 |
| 最近记录: |