<bdo id='ZFEoo'></bdo><ul id='ZFEoo'></ul>

    1. <i id='ZFEoo'><tr id='ZFEoo'><dt id='ZFEoo'><q id='ZFEoo'><span id='ZFEoo'><b id='ZFEoo'><form id='ZFEoo'><ins id='ZFEoo'></ins><ul id='ZFEoo'></ul><sub id='ZFEoo'></sub></form><legend id='ZFEoo'></legend><bdo id='ZFEoo'><pre id='ZFEoo'><center id='ZFEoo'></center></pre></bdo></b><th id='ZFEoo'></th></span></q></dt></tr></i><div id='ZFEoo'><tfoot id='ZFEoo'></tfoot><dl id='ZFEoo'><fieldset id='ZFEoo'></fieldset></dl></div>

      <tfoot id='ZFEoo'></tfoot>
      <legend id='ZFEoo'><style id='ZFEoo'><dir id='ZFEoo'><q id='ZFEoo'></q></dir></style></legend>

      <small id='ZFEoo'></small><noframes id='ZFEoo'>

    2. 子进程“TypeError:需要一个类似字节的对象,而不是'str'"

      时间:2023-07-22
      <tfoot id='O7Uoc'></tfoot>

      <small id='O7Uoc'></small><noframes id='O7Uoc'>

          <legend id='O7Uoc'><style id='O7Uoc'><dir id='O7Uoc'><q id='O7Uoc'></q></dir></style></legend>

          <i id='O7Uoc'><tr id='O7Uoc'><dt id='O7Uoc'><q id='O7Uoc'><span id='O7Uoc'><b id='O7Uoc'><form id='O7Uoc'><ins id='O7Uoc'></ins><ul id='O7Uoc'></ul><sub id='O7Uoc'></sub></form><legend id='O7Uoc'></legend><bdo id='O7Uoc'><pre id='O7Uoc'><center id='O7Uoc'></center></pre></bdo></b><th id='O7Uoc'></th></span></q></dt></tr></i><div id='O7Uoc'><tfoot id='O7Uoc'></tfoot><dl id='O7Uoc'><fieldset id='O7Uoc'></fieldset></dl></div>
              <bdo id='O7Uoc'></bdo><ul id='O7Uoc'></ul>
                  <tbody id='O7Uoc'></tbody>
                本文介绍了子进程“TypeError:需要一个类似字节的对象,而不是'str'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 几年前提出的问题中的这段代码,但是,我认为这已经过时了.尝试运行代码,我收到上面的错误.我仍然是 Python 的新手,所以我无法从类似的问题中得到太多的澄清.有谁知道为什么会这样?

                I'm using this code from a previously asked question a few years ago, however, I believe this is outdated. Trying to run the code, I receive the error above. I'm still a novice in Python, so I could not get much clarification from similar questions. Does anyone know why this is happening?

                import subprocess
                
                def getLength(filename):
                  result = subprocess.Popen(["ffprobe", filename],
                    stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
                  return [x for x in result.stdout.readlines() if "Duration" in x]
                
                print(getLength('bell.mp4'))
                

                追溯

                Traceback (most recent call last):
                  File "B:Program Filesffmpegin	est3.py", line 7, in <module>
                    print(getLength('bell.mp4'))
                  File "B:Program Filesffmpegin	est3.py", line 6, in getLength
                    return [x for x in result.stdout.readlines() if "Duration" in x]
                  File "B:Program Filesffmpegin	est3.py", line 6, in <listcomp>
                    return [x for x in result.stdout.readlines() if "Duration" in x]
                TypeError: a bytes-like object is required, not 'str'
                

                推荐答案

                subprocess 默认为 stdout 或 stderr 流返回 bytes 对象.这意味着您还需要在针对这些对象的操作中使用 bytes 对象."Duration" in x 使用 str 对象.使用字节文字(注意 b 前缀):

                subprocess returns bytes objects for stdout or stderr streams by default. That means you also need to use bytes objects in operations against these objects. "Duration" in x uses str object. Use a bytes literal (note the b prefix):

                return [x for x in result.stdout.readlines() if b"Duration" in x]
                

                或首先解码您的数据,如果您知道使用的编码(通常是语言环境默认设置,但您可以 为子进程设置 LC_ALL 或更具体的语言环境变量):

                or decode your data first, if you know the encoding used (usually, the locale default, but you could set LC_ALL or more specific locale environment variables for the subprocess):

                return [x for x in result.stdout.read().decode(encoding).splitlines(True)
                        if "Duration" in x]
                

                另一种方法是通过将 encoding 参数设置为合适的编解码器来告诉 subprocess.Popen() 将数据解码为 Unicode 字符串:

                The alternative is to tell subprocess.Popen() to decode the data to Unicode strings by setting the encoding argument to a suitable codec:

                result = subprocess.Popen(
                    ["ffprobe", filename],
                    stdout=subprocess.PIPE, stderr = subprocess.STDOUT,
                    encoding='utf8'
                )
                

                如果您设置 text=True(Python 3.7 及更高版本,在以前的版本中,此版本称为 universal_newlines),您还可以使用 系统默认编解码器,和open() 调用.在这种模式下,管道默认是行缓冲的.

                If you set text=True (Python 3.7 and up, in previous versions this version is called universal_newlines) you also enable decoding, using your system default codec, the same one that is used for open() calls. In this mode, the pipes are line buffered by default.

                这篇关于子进程“TypeError:需要一个类似字节的对象,而不是'str'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:并行的 Python 子进程 下一篇:等待进程直到所有子进程完成?

                相关文章

                <tfoot id='QwW7h'></tfoot>
                  <bdo id='QwW7h'></bdo><ul id='QwW7h'></ul>

                  <small id='QwW7h'></small><noframes id='QwW7h'>

                  <i id='QwW7h'><tr id='QwW7h'><dt id='QwW7h'><q id='QwW7h'><span id='QwW7h'><b id='QwW7h'><form id='QwW7h'><ins id='QwW7h'></ins><ul id='QwW7h'></ul><sub id='QwW7h'></sub></form><legend id='QwW7h'></legend><bdo id='QwW7h'><pre id='QwW7h'><center id='QwW7h'></center></pre></bdo></b><th id='QwW7h'></th></span></q></dt></tr></i><div id='QwW7h'><tfoot id='QwW7h'></tfoot><dl id='QwW7h'><fieldset id='QwW7h'></fieldset></dl></div>
                1. <legend id='QwW7h'><style id='QwW7h'><dir id='QwW7h'><q id='QwW7h'></q></dir></style></legend>