Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3 TypeError:需要类似字节的对象,而不是';str';_Python_C++_Python 3.x_Opencv - Fatal编程技术网

Python3 TypeError:需要类似字节的对象,而不是';str';

Python3 TypeError:需要类似字节的对象,而不是';str';,python,c++,python-3.x,opencv,Python,C++,Python 3.x,Opencv,我试图遵循这个OpenCV练习,但在运行mergevec.py的步骤上遇到了困难(我使用Python版本而不是.cpp版本)。我使用的是Python3,而不是本文中提到的Python2.x 此文件的源是 我犯的错误是 Traceback (most recent call last): File "./tools/mergevec1.py", line 96, in <module> merge_vec_files(vec_directory, output_filena

我试图遵循这个OpenCV练习,但在运行mergevec.py的步骤上遇到了困难(我使用Python版本而不是.cpp版本)。我使用的是Python3,而不是本文中提到的Python2.x

此文件的源是

我犯的错误是

Traceback (most recent call last):
  File "./tools/mergevec1.py", line 96, in <module>
    merge_vec_files(vec_directory, output_filename)
  File "./tools/mergevec1.py", line 45, in merge_vec_files
    val = struct.unpack('<iihh', content[:12])
TypeError: a bytes-like object is required, not 'str'
你知道我做错了什么吗?谢谢

更新1

我这样做:

content = b''.join(str(line) for line in vecfile.readlines())
我基本上在前面加了“b”。但是,现在我得到了一个不同的错误:

回溯(最近一次呼叫最后一次): 文件“/tools/mergevec1.py”,第97行,在 合并向量文件(向量目录,输出文件名) 文件“/tools/mergevec1.py”,第44行,在merge_vec_文件中 content=b“”。连接(str(line)表示vecfile.readlines()中的行)
TypeError:序列项0:应为类似字节的对象,str found

正如OP所解释的,该文件包含二进制数据。要使用二进制数据,请执行以下操作:

  • 文件应以二进制模式打开,使用
    'rb'
    作为 在
    打开
    调用中的模式
  • 打开文件后,使用
    .read()
    而不是
    .readlines()
    来读取数据。这避免了可能的错误
    .readlines()
    处理行的方式导致数据损坏 结尾字符
  • 避免将字节数组转换为字符串的操作,如
    .join()
    字符数组(字符串)
  • 对于问题中提供的代码,用于读取图像的代码部分应为:

    for f in files:
        try:
            with open(f, 'rb') as vecfile:
                content = vecfile.read()
                val = struct.unpack('<iihh', content[:12])
                num_images = val[0]
                image_size = val[1]
                if image_size != prev_image_size:
                    err_msg = """The image sizes in the .vec files differ. These values must be the same. \n The image size of file {0}: {1}\n 
                        The image size of previous files: {0}""".format(f, image_size, prev_image_size)
                    sys.exit(err_msg)
    
                total_num_images += num_images
        except IOError as e:
            print('An IO error occured while processing the file: {0}'.format(f))
            exception_response(e)
    
    对于文件中的f:
    尝试:
    打开(f,'rb')作为矢量文件:
    content=vecfile.read()
    
    val=struct.unpack(“正如OP所解释的,文件包含二进制数据。要使用二进制数据:

  • 文件应以二进制模式打开,使用
    'rb'
    作为 在
    打开
    调用中的模式
  • 打开文件后,使用
    .read()
    而不是
    .readlines()
    来读取数据。这避免了可能的错误
    .readlines()
    处理行的方式导致数据损坏 结尾字符
  • 避免将字节数组转换为字符串的操作,如
    .join()
    字符数组(字符串)
  • 对于问题中提供的代码,用于读取图像的代码部分应为:

    for f in files:
        try:
            with open(f, 'rb') as vecfile:
                content = vecfile.read()
                val = struct.unpack('<iihh', content[:12])
                num_images = val[0]
                image_size = val[1]
                if image_size != prev_image_size:
                    err_msg = """The image sizes in the .vec files differ. These values must be the same. \n The image size of file {0}: {1}\n 
                        The image size of previous files: {0}""".format(f, image_size, prev_image_size)
                    sys.exit(err_msg)
    
                total_num_images += num_images
        except IOError as e:
            print('An IO error occured while processing the file: {0}'.format(f))
            exception_response(e)
    
    对于文件中的f:
    尝试:
    打开(f,'rb')作为矢量文件:
    content=vecfile.read()
    
    val=struct.unpack(“当我更改我的问题时,我能够解决它:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = ''.join(str(line) for line in vecfile.readlines())
                    data = content[12:]
                    outputfile.write(data)
    except Exception as e:
        exception_response(e)
    
    content = ''.join(str(line) for line in vecfile.readlines())
    
    对于it:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = b''.join((line) for line in vecfile.readlines())
                    outputfile.write(bytearray(content[12:]))
    except Exception as e:
        exception_response(e)
    
    content = b''.join((line) for line in vecfile.readlines())
    
    就像我改变它之前一样:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = ''.join(str(line) for line in vecfile.readlines())
                    data = content[12:]
                    outputfile.write(data)
    except Exception as e:
        exception_response(e)
    
    content = ''.join(str(line) for line in vecfile.readlines())
    
    对于it:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = b''.join((line) for line in vecfile.readlines())
                    outputfile.write(bytearray(content[12:]))
    except Exception as e:
        exception_response(e)
    
    content = b''.join((line) for line in vecfile.readlines())
    
    因为它在等待一些str,现在它能够接收我们需要的二进制文件

    您正在保留错误,因为您正在使用代码

    content = b''.join(str(line) for line in vecfile.readlines())
    
    您必须使用:

    content = b''.join((line) for line in vecfile.readlines())
    

    这是没有“str”类型的。

    当我更改它时,我能够解决我的问题:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = ''.join(str(line) for line in vecfile.readlines())
                    data = content[12:]
                    outputfile.write(data)
    except Exception as e:
        exception_response(e)
    
    content = ''.join(str(line) for line in vecfile.readlines())
    
    对于it:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = b''.join((line) for line in vecfile.readlines())
                    outputfile.write(bytearray(content[12:]))
    except Exception as e:
        exception_response(e)
    
    content = b''.join((line) for line in vecfile.readlines())
    
    就像我改变它之前一样:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = ''.join(str(line) for line in vecfile.readlines())
                    data = content[12:]
                    outputfile.write(data)
    except Exception as e:
        exception_response(e)
    
    content = ''.join(str(line) for line in vecfile.readlines())
    
    对于it:

    for f in files:
                with open(f, 'rb') as vecfile:
                    content = b''.join((line) for line in vecfile.readlines())
                    outputfile.write(bytearray(content[12:]))
    except Exception as e:
        exception_response(e)
    
    content = b''.join((line) for line in vecfile.readlines())
    
    因为它在等待一些str,现在它能够接收我们需要的二进制文件

    您正在保留错误,因为您正在使用代码

    content = b''.join(str(line) for line in vecfile.readlines())
    
    您必须使用:

    content = b''.join((line) for line in vecfile.readlines())
    

    没有“str”cast.

    您是否按照您所引用问题的答案中的建议,尝试使用模式
    'rb'
    打开文件?您可能还需要删除
    编码部分,因为您只是读取字节,编码不相关。是的
    rb
    是最初导致错误的原始代码。我也尝试了
    open(files[0],'r',errors='ignore')
    基于您刚才的建议,仍然是相同的错误。
    rb
    是正确的。然后在下一行中再次将字节转换为字符串。您的文件的结构是什么?为什么要加入行?使用
    content=vecfile.read()怎么样
    而不是
    “”。join(…)
    ?它们基本上是二进制格式(.vec文件)。参考:如果它们是二进制格式,那么
    vecfile.read()
    应该可以解决您的问题。使用
    vecfile.readlines()
    可能会损坏文件内容。您是否按照您所引用问题的答案中的建议,尝试使用模式
    'rb'
    打开文件?您可能还需要删除
    编码部分,因为您只是读取字节,而编码是不相关的。是
    rb
    是原始代码,也导致了错误首先。根据您刚才的建议,我还尝试了
    open(files[0],'r',errors='ignore')
    ,但仍然是相同的错误。
    rb
    是正确的。然后在下一行中再次将字节转换为字符串。您的文件的结构是什么?为什么要加入行?使用
    content=vecfile.read()如何
    而不是
    '.join(…)
    ?它们基本上是二进制格式(.vec文件)。参考:如果它们是二进制格式,那么
    vecfile.read()
    应该可以解决您的问题。使用
    vecfile.readlines()
    可能会损坏文件内容。