为什么预期的字符串变成元组[重复]

2024-04-08

我预计变量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)

Your output_format始终是元组,因为您在每个可能的值后面放置了一个逗号:

output_format = "..." % fname,
# ---------------------------^

删除那些逗号和你的cmd_str将再次仅包含字符串。

Python 元组由此类逗号组成;仅当不使用括号会导致歧义时才需要括号:

>>> 1
1
>>> 1,
(1,)
>>> type(_)
<class 'tuple'>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么预期的字符串变成元组[重复] 的相关文章

随机推荐