0

I have following code writing CSV data to a file

with open('data-file.csv','wb') as myfile:
  wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
  data = [time.strftime('%H:%M:%S'),int(round((value2 - value) / 1, 2))]
  wrtr.writerow(data)

I am retrieving below error, I tried to solve it but I am unable

 wrtr.writerow(data)
 TypeError: a bytes-like object is required, not 'str'
eman davis
  • 23
  • 8

1 Answers1

-1

I am not sure since I can not reproduce the code with any data example, but the problem probably lies within the open method, you chose 'wb' write binary. Therefore you need to provide binary data(not string data). I will not go into detail because this has already been done in other posts. As solution choose a different opening style like 'w' or 'w+', this different opening style they says that you can also provide text data in this case string objects.

Link to related stackoverflow

Link to Python documentation