[英]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)))
Getting error in cf.write(chr(int(c1,2)))在 cf.write(chr(int(c1,2))) 中出现错误
TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”
Code was running fine on python 2.x代码在 python 2.x 上运行良好
In your comment you said that you were opening the file as a bytes by using the argument wb
however you're trying to write a string
to the file which causes the error.在您的评论中,您说您使用参数
wb
将文件作为字节打开,但是您试图将string
写入导致错误的文件。
To fix that all you have to do is convert the string to bytes要解决这个问题,您只需将字符串转换为字节
for d in range(0,d2.size):
c2 += str(d2.item(d))
cf.write(bytes(chr(int(c2,2)), 'utf-8'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.