[英]Python 3 upgrade, a bytes-like object is required, not 'str'
I've been trying to get this work but getting the error TypeError: a bytes-like object is required, not 'str''
after upgrading to python 3. 我一直在尝试完成这项工作,但收到错误TypeError: a bytes-like object is required, not 'str''
升级到python 3后TypeError: a bytes-like object is required, not 'str''
。
What am I doing wrong here? 我在这里做错了什么? I tried r
, wb+
and w
learned from here, Confused by python file mode "w+" 我尝试过r
, wb+
和w
从这里学到的, 被python文件模式“ w +”弄糊涂了
my code: 我的代码:
with open(output_filename, 'wb') as f:
# write column names
f.write("stack,overflow,super,user\n")
writer = csv.writer(f)
Can anyone help with this? 有人能帮忙吗? Thanks. 谢谢。
The difference between 'wb' and 'w' filemodes is that 'wb' directly reads the binary and 'w' reads it as string. “ wb”和“ w”文件模式之间的区别在于,“ wb”直接读取二进制文件,而“ w”将其读取为字符串。 Your issue is that you're using 'wb' instead of 'w'. 您的问题是您使用的是'wb'而不是'w'。 csv.writer is expecting a string, not binary. csv.writer需要一个字符串,而不是二进制。
If you use with open(output_filename, 'w') as f:
instead, it should work. 如果您将with open(output_filename, 'w') as f:
使用,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.