简体   繁体   中英

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.

What am I doing wrong here? I tried r , wb+ and w learned from here, Confused by python file mode "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. Your issue is that you're using 'wb' instead of 'w'. csv.writer is expecting a string, not binary.

If you use with open(output_filename, 'w') as f: instead, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM