[英]TypeError: a bytes-like object is required, not 'str' when writing to a 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:需要類似字節的 object,而不是“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'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.