base64 type err, typeerror: expected bytes, not str

  • Last Update :
  • Techknowledgy :

Encoded bytes will do.

bytes(base64_code, encoding = 'utf-8')

Suggestion : 2

Posted by Marta on March 24, 2021 Viewed 63748 times

product;
stock;
price
bike;
10;
50
laptop;
3;
1000
microphone;
5;
40
book;
3;
9
with open('file_sample.txt', 'rb') as f:
   lines = [x.strip() for x in f.readlines()]

bike_available = False
for line in lines:
   tmp = line.strip().lower()
split_line = tmp.split(';')
if split_line[0] == 'bike'
and int(split_line[1]) > 0:
   bike_available = True

print("Bikes Available? " + str(bike_available))
Traceback (most recent call last):
Error line 7, in <module>
   splitted_line = tmp.split(';')
   TypeError: a bytes-like object is required, not 'str'
with open('file_sample.txt', 'rb') as f:
   lines = [x.strip() for x in f.readlines()]

bike_available = False
for line in lines:
   tmp = line.strip().lower()
split_line = tmp.split(b ';') # Added the b prefix
if split_line[0] == b 'bike'
and int(split_line[1]) > 0:
   bike_available = True

print("Bikes Available? " + str(bike_available))
Bikes Available ? True
with open('file_sample.txt', 'r') as f:
   lines = [x.strip() for x in f.readlines()]

bike_available = False
for line in lines:
   tmp = line.strip().lower()
split_line = tmp.split(';')
if split_line[0] == 'bike'
and int(split_line[1]) > 0:
   bike_available = True

print("Bikes Available? " + str(bike_available))

Suggestion : 3

Deprecated alias of anycodings_odoo-11 decodebytes(). Deprecated since version 3.1.,I don't know at all what kind of anycodings_python variable is result.ByteData, so I am not anycodings_python sure if the encode() method applies anycodings_python here.,I'm trying decodebytes() instead of anycodings_odoo-11 decodestring() but it's not working.,In my case, the error was:

I upgraded Odoo v11's python2 to python3. anycodings_odoo-11 After that I edited my addons. One of my anycodings_odoo-11 addons, i'm getting error.

result = method(recs, * args, ** kwargs)
File "D:\Odoo 11.0\server\addons\addons- 
trbase\ l10n_tr_account_einvoice\ models\ account_einvoice_provider.py ", 
line 698, in action_einvoice_get_invoices
self.einvoice_get_invoices()
File "D:\Odoo 11.0\server\addons\addons- unauth\l10n_tr_account_einvoice_provider_isis\models\account_einvoice_provider.py", line 272, in einvoice_get_invoices
bytedata = base64.decodestring(result.ByteData)
File "D:\Odoo 11.0\python\lib\base64.py", line 552, in decodebytes
_input_type_check(s)
File "D:\Odoo 11.0\python\lib\base64.py", line 520, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes - like object, not Text

Here is my class method:

def einvoice_get_invoices(self):
   if self.provider != 'isis':
   return super(AccountEinvoiceProvider, self).einvoice_get_invoices()
else:
   try:
   client = self.isis_get_client()
count = 0
while count < 50:
   count += 1
result = client.service.GetSingleEnvelope(self.company_id.vat[2: ])
self.isis_check_error(result)
if result.EnvelopeUUID:
   bytedata = base64.decodestring(result.ByteData)
buffer = io.BytesIO(bytedata)
if zipfile.is_zipfile(buffer):
   file = zipfile.ZipFile(buffer, 'r')
for name in file.namelist():
   bytedata = file.read(name)
_logger.debug("Processing Envelope: %s" % bytedata.decode('utf-8'))
self.einvoice_process_envelope(bytedata)
else:
   _logger.info("Invalid Zip File! EnvelopeUUID= %s" % result.EnvelopeUUID)
else:
   count = 50
return True
except WebFault as e:
   _logger.error(_('E-Invoice Provider WebService Error!') + '\n\n' + e.message)
return False

In my case, the error was:

TypeError: expected bytes - like object, not 'str'

and the solution is to encode the anycodings_python (unicode) string before use it:

bytedata = base64.decodestring(result.ByteData.encode())