Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string argument without an encoding

Am trying to a run this piece of code, and it keeps giving an error saying "String argument without an encoding"

ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '\0'.encode('utf-8')

Any help?

like image 228
lonely Avatar asked Jul 01 '15 12:07

lonely


1 Answers

You are passing in a string object to a bytearray():

bytearray(content[current_pos:(final_pos)])

You'll need to supply an encoding argument (second argument) so that it can be encoded to bytes.

For example, you could encode it to UTF-8:

bytearray(content[current_pos:(final_pos)], 'utf8')

From the bytearray() documentation:

The optional source parameter can be used to initialize the array in a few different ways:

  • If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
like image 133
Martijn Pieters Avatar answered Nov 03 '22 00:11

Martijn Pieters