Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select and Convert bytes object from a list

I have tried everything in my mind but wasn’t successfull. Can anybody help me to select bytes object from a list bellow?

[('uid=xxxxxx,o=center', {'userCertificate;binary': [b'0\x82\x07q0\x82\x05Y\xa0\x03\x02\x01\x02\x02\x10`\ .....']})]

This is the result from a openldap search and I want to select the userCertificate part of the entry and convert it to a base64 text. When I manually select only the bytes object – the b’0\x82\x07q0\x82\x05Y\xa0\x03\x02\x01\x02\x02\x10`\ …..’ section I am able to convert it with base64.b64encode(bytes_variable) into a certificate string, but I have to automatize the selection of this bytes object. I have tried to convert this list into string and use regex to pickup only the part with b”, but then I am not able to convert it to correct certificate format. Any ideas how to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have a Python list containing a single tuple. The 2nd element of that tuple is a dictionary. That dictionary has a single key. Its value is a list containing one item.

So…

list_ = [('uid=xxxxxx,o=center', {'userCertificate;binary': [b'0\x82\x07q0\x82\x05Y\xa0\x03\x02\x01\x02\x02\x10']})]

bytes_object = list_[0][1]['userCertificate;binary'][0]

Note:

bytes truncated to make this syntactically correct

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading