Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
File Python读取和写入二进制文件_File_Python 3.x_Binary - Fatal编程技术网

File Python读取和写入二进制文件

File Python读取和写入二进制文件,file,python-3.x,binary,File,Python 3.x,Binary,以下是我重新措辞的问题 读取二进制文件的前10个字节(以后的操作)- 第一个print语句给出- 255, 216, 255, 224, 0, 16, 74, 70, 73, 70, b'\xff\xd8\xff\xe0\x00\x10JFIF' b'\xff\xd8\xff\xe0\x00\x10JFIF' [255, 216, 255, 224, 0, 16, 74, 70, 73, 70] 第二个print语句给出- 255, 216, 255, 224, 0, 16, 74,

以下是我重新措辞的问题

读取二进制文件的前10个字节(以后的操作)-

第一个print语句给出-

255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 
b'\xff\xd8\xff\xe0\x00\x10JFIF'
b'\xff\xd8\xff\xe0\x00\x10JFIF'
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70]
第二个print语句给出-

255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 
b'\xff\xd8\xff\xe0\x00\x10JFIF'
b'\xff\xd8\xff\xe0\x00\x10JFIF'
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70]
对x中值的十六进制解释

outfile.write(bytes(x, "UTF-8"))
返回-

TypeError: encoding or errors without a string argument
那么x不能是一个普通字符串,而是一个字节字符串,它仍然是可编辑的

如果我想将x的内容原封不动地写入outfile.jpg,那么我就去-

outfile.write(x)
现在,我尝试获取每个x[I]并对每个x[I]执行一些操作(如下所示为1的简单乘积),将值分配给y,并将y写入outfile.jpg,使其与infile.jpg相同。所以我试着-

infile = open('infile.jpg', 'rb')
outfile = open('outfile.jpg', 'wb')
x = infile.read(10)

yi = len(x)
y = [0 for i in range(yi)]

j = 0
for i in x:
    y [j] = i*1
    j += 1

for i in x:
    print(i, end=', ')

print(x)

for i in y:
    print(i, end=', ')

print(y)

print(repr(x))
print(repr(y))

outfile.write(y)
第一个print语句(遍历x)给出-

第二个print语句给出-

255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 
b'\xff\xd8\xff\xe0\x00\x10JFIF'
b'\xff\xd8\xff\xe0\x00\x10JFIF'
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70]
第三个print语句(遍历y)给出-

print语句给出了-

255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 
b'\xff\xd8\xff\xe0\x00\x10JFIF'
b'\xff\xd8\xff\xe0\x00\x10JFIF'
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70]
最后,按照Tim的建议,打印repr(x)和repr(y)分别给出-

b'\xff\xd8\xff\xe0\x00\x10JFIF'
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70]
而file write语句给出了错误-

TypeError: 'list' does not support the buffer interface
我需要的是y与x的类型相同,这样outfile.write(x)=outfile.write(y)


我凝视着巨蟒的眼睛,但仍然看不到它的灵魂。

它们一点都不相同-它们只是在应用了
str()
之后显示相同(这是
print()
隐含的)。打印它们的
repr()
,您将看到它们的区别。例如:

>>> x = b'ab'
>>> y = "b'ab'"
>>> print(x)
b'ab'
>>> print(y) # displays identically
b'ab'
>>> print(repr(x)) # but x is really a 2-byte bytes object
b'ab'
>>> print(repr(y)) # and y is really a 5-character string
"b'ab'"
混合使用字符串和字节对象是没有意义的(好吧,在没有显式编码的情况下是没有意义的,但是在这里你没有尝试编码/解码任何东西,对吧?)。如果您使用的是二进制文件,那么就不应该使用字符串-应该使用
字节
字节数组
对象

所以问题并不在于你如何写作:在那之前,逻辑基本上是混乱的

猜不出你想要什么。请编辑问题,以显示一个完整的、可执行的示例,说明您正试图实现的目标。我们不需要JPG文件来实现这一点——它可以组成一些简短、任意的二进制数据。比如:

dummy_jpg = b'\x01\x02\xff'

。。。这就是以二进制模式在Python中读写文件的方式。

#open binary files infile and outfile
infile = open('infile.jpg', 'rb')
outfile = open('outfile.jpg', 'wb')

#n = bytes to read
n=5

#read bytes of infile to x
x = infile.read(n)

#print x type, x
print()
print('x = ', repr(x), type(x))
print()
x=b'\xff\xd8\xff\xe0\x00'类“字节”

#define y of type list, lenth xi, type list
xi = len(x)
y = [0 for i in range(xi)]

#print y type, y
print('y =', repr(y), type(y))
print()
#conclusion:  first n bytes of infile = n bytes of outfile (without bit level operations)

outfile.close()
y=[0,0,0,0,0]类“列表”

#convert x to 8 bit octals and place in y, type list
j=0
for i in x:
    y [j] = '{:08b}' .format(ord(i))
    j += 1

#print y type, and y
print('y =', repr(y), type(y))
print()
#perform bit level operations on y [i], not done in this example.

#convert y [i] back to integer
j=0
for i in y:
    y [j] = int(i, 2)
    j += 1

#print y type, and y
print('y =', repr(y), type(y))
print()
#convert y to type byte and place in z
z = bytearray(y)

#print z type, and z
print('z =', repr(z), type(z))
print()
y=['11111111','11011000','11111111','11100000','00000000']类“列表”

#convert x to 8 bit octals and place in y, type list
j=0
for i in x:
    y [j] = '{:08b}' .format(ord(i))
    j += 1

#print y type, and y
print('y =', repr(y), type(y))
print()
#perform bit level operations on y [i], not done in this example.

#convert y [i] back to integer
j=0
for i in y:
    y [j] = int(i, 2)
    j += 1

#print y type, and y
print('y =', repr(y), type(y))
print()
#convert y to type byte and place in z
z = bytearray(y)

#print z type, and z
print('z =', repr(z), type(z))
print()
y=[255,216,255,224,0]类“列表”

#convert x to 8 bit octals and place in y, type list
j=0
for i in x:
    y [j] = '{:08b}' .format(ord(i))
    j += 1

#print y type, and y
print('y =', repr(y), type(y))
print()
#perform bit level operations on y [i], not done in this example.

#convert y [i] back to integer
j=0
for i in y:
    y [j] = int(i, 2)
    j += 1

#print y type, and y
print('y =', repr(y), type(y))
print()
#convert y to type byte and place in z
z = bytearray(y)

#print z type, and z
print('z =', repr(z), type(z))
print()
z=bytearray(b'\xff\xd8\xff\xe0\x00')类“bytearray”

#output z to outfile
outfile.write(z)

infile.close()
outfile.close()
outfile = open('outfile.jpg', 'rb')

#read bytes of outfile to x
x = outfile.read(n)

#print x type, and x
print('x =', repr(x), type(x))
print()
x=b'\xff\xd8\xff\xe0\x00'类“字节”

#define y of type list, lenth xi, type list
xi = len(x)
y = [0 for i in range(xi)]

#print y type, y
print('y =', repr(y), type(y))
print()
#conclusion:  first n bytes of infile = n bytes of outfile (without bit level operations)

outfile.close()

谢谢你的澄清!你想要的很简单,但是你确实需要阅读
字节
字节数组
类型的文档。你不想要的东西与以下方面有关:

  • 统一码
  • 编码
  • 解码
这些在这里完全不相干。您自始至终都有二进制数据,需要坚持使用
bytes
和/或
bytearray
对象。两者都是字节序列(“小整数”位于
范围(256)
bytes
是一个不可变序列,
bytearray
是一个可变序列

那么x不能是一个普通字符串,而是一个字节字符串,它仍然是可编辑的

阅读文档;-)<代码>x不是“字符串”;执行此操作以查看其类型:

print(type(x))
这将显示:

<class 'bytes'>

然后

我会做你想做的

虽然,如上所述,我不知道您为什么要在这里创建一个列表。创建相同列表的一种更简单的方法是跳过所有循环,只需编写:

 y = list(x)

如果我通过了,你应该开始怀疑你的心理模型太复杂了,而不是太简单了。你想象的困难其实并不存在:-)从二进制文件中读取会给你一个
字节
对象(如果你想读取二进制文件来填充
字节
对象,请参阅file
.readinto()
方法),写入二进制文件时,需要给它一个
bytes
bytearray
对象进行写入。仅此而已。

看看这篇文章:似乎Python 2和Python 3之间的字符串类发生了变化。Hunter-我用outfile.write(s.encode('UTF-8')替换了outfile.write(s),没有收到任何错误!但是使用infle.read()导致outfile.jpg的大小是infile.jpg的两倍并被破坏。我试图完成的是读取一个二进制文件,执行一个操作,反转该操作,并将输出写入一个单独的文件,使其完全相同。我链接的帖子中的答案使用了
outfile.write(字节,“UTF-8”);
Wow,repr()我将不得不重新思考我试图做的事情的逻辑。