为什么期望字符串变成元组 [英] Why expected string become an tuple

查看:33
本文介绍了为什么期望字符串变成元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望变量 output_format 是一个字符串.但是当我运行脚本时,它给了我一个 tuple 类型并抛出异常.

如果我在 Python 解释器中运行,它会给我一个预期的字符串.

('--sout "#standard{access=file,vcodec=h264,dst=c0_s0_h264_640x480_30_vbr_500_99_40000000.mp4}"',)'h264'<输入'str'>回溯(最近一次调用最后一次):运行中的文件streaming_verification/src/streaming_verification/scripts/streaming_verification.py",第 184 行self.streaming.dump_file(export_fname, 5, codec_type)文件streaming_verification/src/streaming_verification/scripts/streaming.py",第 57 行,在 dump_file 中cmd_str = " ".join(cmd)类型错误:序列项 3:预期的字符串,找到元组

脚本源代码:

def dump_file(self,fname='',周期=10,codec_type="h264"):如果 "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(输出格式)指令 =["vlc","-我假的","--rtsp-tcp {0}".format(self.conn_cfg["rtsp_link"]),输出格式,"--stop-time {0} vlc://quit ".format(period)]cmd_str = " ".join(cmd)self.run(cmd_str)

解决方案

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

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

去掉那些逗号,你的 cmd_str 将再次只包含字符串.

Python 元组就是由这样的逗号构成的;只有在不使用它们会导致歧义时才需要括号:

<预><代码>>>>11>>>1、(1,)>>>类型(_)<类'元组'>

I expected the variable output_format to be a string. But when I ran the script it gave me a tuple type and threw an exception.

If I run in Python interpreter, it gave me an expected string.

('--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

Script source code:

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 is always tuple, because you put a comma after each possible value:

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

Remove those commas and your cmd_str will once again only contain strings.

Python tuples are formed by such commas; the parenthesis are only needed when not using them would lead to ambiguity:

>>> 1
1
>>> 1,
(1,)
>>> type(_)
<class 'tuple'>

这篇关于为什么期望字符串变成元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆