[英]Python 3 and TypeError: a bytes-like object is required, not 'str' error
[英]Python 3 upgrade, a bytes-like object is required, not 'str'
我一直在嘗試完成這項工作,但收到錯誤TypeError: a bytes-like object is required, not 'str''
升級到python 3后TypeError: a bytes-like object is required, not 'str''
。
我在這里做錯了什么? 我嘗試過r
, wb+
和w
從這里學到的, 被python文件模式“ w +”弄糊塗了
我的代碼:
with open(output_filename, 'wb') as f:
# write column names
f.write("stack,overflow,super,user\n")
writer = csv.writer(f)
有人能幫忙嗎? 謝謝。
“ wb”和“ w”文件模式之間的區別在於,“ wb”直接讀取二進制文件,而“ w”將其讀取為字符串。 您的問題是您使用的是'wb'而不是'w'。 csv.writer需要一個字符串,而不是二進制。
如果您將with open(output_filename, 'w') as f:
使用,它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.