我想要写入两个单独的二进制文件(即,满足条件的行的n/2个必须到达文件,并且必须将该矩阵的大小设为(n*n
)留给另一个)。我写的如下面的代码:向二进制文件写入列表时出现
def write_to_binary_file(X, y):
posiResponse = []
negaResponse = []
for idx, res in enumerate(y):
if res == 1:
posiResponse.extend(X[idx])
else:
negaResponse.extend(X[idx])
with open("POS.bin", mode='wb') as file1:
file1.write(bytearray(posiResponse))
file1.close()
with open("NEG.bin", mode='wb') as file2:
file2.write(bytearray(negaResponse))
file2.close()
我得到的抱怨阵列和如何我用bytearray()
但我不知道如何调整它的错误:
Traceback (most recent call last):
File "exper.py", line 173, in <module>
write_data(X, y)
File "exper.py.py", line 47, in write_data
file1.write(bytearray(posiResponse))
TypeError: an integer or string of size 1 is required
请,可有人提供了一个很好的修复谢谢。
你想写一个numpy的阵列到一个二进制文件?你关心它是如何完成的? –