简体   繁体   English

类型错误:将字符串写入文件需要类似字节的 object,而不是“str”

[英]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.

相关问题 类型错误:写入文件时需要类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' when writing to a file 需要一个类似字节的对象,而不是'str':TypeError - a bytes-like object is required, not 'str' : TypeError TypeError: bytes-like object 是必需的,而不是 'str' - TypeError: bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是'str' - TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”? - TypeError: a bytes-like object is required, not 'str'? 将CSV写入临时文件时出现“ TypeError:需要一个类似字节的对象,而不是'str'” - “TypeError: a bytes-like object is required, not 'str'” when writing a CSV to a temporary file “TypeError:在 Python 3 中处理文件内容时,需要类似字节的 object,而不是‘str’” - "TypeError: a bytes-like object is required, not 'str'" when handling file content in Python 3 TypeError:需要一个类似字节的对象,而不是“ str”,但类型是“ bytes” - TypeError: a bytes-like object is required, not 'str' but type is 'bytes' TypeError:需要类似字节的 object,不是“str”,但类型显示字节 - TypeError: a bytes-like object is required, not 'str' but type shows bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM