Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string - Python 3.5 write a bytes-like object is required, not 'str'

My following python code works for Python 2,

Write header only once

if header_written == False:
    header = out_data.keys()
    writer.writerow(out_data.keys()) # write headers
    header_written = True

Write values

writer.writerow(out_data.values()) #write rows
del out_data  #del object
del row_data #del dict object

but in Python 3, it returns the following error:

TypeError: a bytes-like object is required, not 'str'

like image 297
Chenxi Avatar asked Aug 21 '18 00:08

Chenxi


1 Answers

You have to convert it to bytes. You can do it like this.

bytes = string.encode(encoding='UTF-8')

More info here

Best way to convert string to bytes in Python 3?

like image 182
Deejpake Avatar answered Oct 03 '22 01:10

Deejpake