TypeError: a bytes-like object is required, not 'str', for writing string to file
TypeError: a bytes-like object is required, not 'str', for writing string to file
for d in range(0,d2.size):
c2 += str(d2.item(d))
cf.write(chr(int(c2,2)))
在 cf.write(chr(int(c1,2)))
中出现错误
TypeError: 需要类似字节的对象,而不是 'str'
代码在 python 2.x
上 运行 没问题
在您的评论中,您说您使用参数 wb
将文件作为字节打开,但是您正试图将 string
写入导致错误的文件。
要解决这个问题,您只需将字符串转换为字节
for d in range(0,d2.size):
c2 += str(d2.item(d))
cf.write(bytes(chr(int(c2,2)), 'utf-8'))
for d in range(0,d2.size):
c2 += str(d2.item(d))
cf.write(chr(int(c2,2)))
在 cf.write(chr(int(c1,2)))
中出现错误TypeError: 需要类似字节的对象,而不是 'str'
代码在 python 2.x
上 运行 没问题在您的评论中,您说您使用参数 wb
将文件作为字节打开,但是您正试图将 string
写入导致错误的文件。
要解决这个问题,您只需将字符串转换为字节
for d in range(0,d2.size):
c2 += str(d2.item(d))
cf.write(bytes(chr(int(c2,2)), 'utf-8'))