简体   繁体   English

Python 3升级,需要一个类似字节的对象,而不是'str'

[英]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+" 我尝试过rwb+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.

相关问题 Python 3和TypeError:需要一个类似字节的对象,而不是'str'错误 - Python 3 and TypeError: a bytes-like object is required, not 'str' error Python TypeError:需要一个类似字节的对象,而不是'str' - Python TypeError: a bytes-like object is required, not 'str' python3 TypeError:需要一个类似字节的对象,而不是'str' html - python3 TypeError: a bytes-like object is required, not 'str' html TypeError:需要一个类似字节的对象,而不是'str'python3 - TypeError: a bytes-like object is required, not 'str' python3 类型错误:需要一个类似字节的对象,而不是 python 和 CSV 中的“str” - TypeError: a bytes-like object is required, not 'str' in python and CSV TypeError:需要一个类似字节的对象,而不是python 3.5中的“ str” - TypeError: a bytes-like object is required, not 'str' in python 3.5 字符串 - Python 3.5 需要写一个类似字节的对象,而不是“str” - string - Python 3.5 write a bytes-like object is required, not 'str' Python 3 - TypeError:需要类似字节的对象,而不是'str' - Python 3 - TypeError: a bytes-like object is required, not 'str' 需要一个类似字节的对象,在pickle python 3.6中不需要'str' - a bytes-like object is required, no 'str' with pickle python 3.6 Python:windows 上的子进程 + isql:需要类似字节的 object,而不是“str” - Python: subprocess + isql on windows: a bytes-like object is required, not 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM