1

So in python, you can write data to a file in bytes like this

with open(filename, 'wb') as f:
        f.write(data)

I have an api which does a cat on this file, and gives back the data looking like the example at the bottom

here is the code that im having trouble with

r = requests.get("http://ip/read/octo/exampleapikey123456789")
a = r.text
a = b''.join(a)

AMS.readMsg('private_octo.pem',a)

I get this error

TypeError: sequence item 0: expected a bytes-like object, str found

My question is, how do i convert the following string, into a 'bytes-like object)

Thanks :)

"b't\\xf8O\\xfc\\xa5\\xa1,\\x93\\xfeJ\\xd5>\\xf9\\x00\\xa9\\xd1\\xe1`\\x15\\xfe\\x88\\x0c\\xbf\\xc8!\\xfe\\xcd\\x007\\x14\\x05\\xd6\\xa5\\xbb\\nus\\x18\\x02\\x90\\xb9\\xd3\\x89\\x03\\xe3_@\\xf8\\xe1\\xa3\\x87\\x0c\\xe4)\\xb4\\xfd\\xe7\\xb2\\xdb\\x02~\\xeb\\x1a\\xba\\xa5\\xfc\\x90\\x88v\\xbe\\r\\x12bE\\x94\\xfe\\xdcI\"T\\x14\\x99%\\x862\\x9b}H\\xb74/\\x8f\\xaf\\xeae\\xe3\\xdb\\xd4\\x0cAc\\xc3\\xd3H\\xd6\\x00o\\xa8\\xf23DbM\\xfd\\x80+,Q\\xb5\\xc6\\xae\\r\\x06{+\\x1b$\\xb4\\xb8\\x8a\\xe0\\xec\\xfb\\xc0^}\\x85=\\xba\\xa6\\xa3L\\x17\\xbd;\\xe2\\xe0$\\xb0:\\x9bi\\x06\\x14\\xe5\\x8a-\\x95+\\nRK\\x97\\xdcz\\xbe\\x18\\x86Z\\xa6%k\\xfb\\x958X\\xa1P\\xf7\\x01\\x06\\xcd\\x02.\\x9bTt\\x8dzkU\\xe5V\\xb8>\\'7\\xf4\\x14%\\xc3W#\\x96\\x8b.\\x1cx\\'\\xae\\xa1\\xb8i&\\x11\\'\\xb5)\\xc2\\x8f\\x84\\x16N\\x8a\\xb5\\nC0!A\\x96y\\xfc\\xa3\\xf0\\x935\\xb2x9U\\x9a\\xe9\\xf9\\xee\\xfa\\xce\\x13\\x9a1\\xd5\\x99\\xf2\\xfd\\xbf\\x80'"

1 Answer 1

1

You can use ast.literal_eval:

>>> obj = ast.literal_eval("b't\\xf8O\\xfc\\xa5\\xa1,\\x93\\xfeJ\\xd5>\\xf9\\x00\\xa9\\xd1\\xe1`\\x15\\xfe\\x88\\x0c\\xbf\\xc8!\\xfe\\xcd\\x007\\x14\\x05\\xd6\\xa5\\xbb\\nus\\x18\\x02\\x90\\xb9\\xd3\\x89\\x03\\xe3_@\\xf8\\xe1\\xa3\\x87\\x0c\\xe4)\\xb4\\xfd\\xe7\\xb2\\xdb\\x02~\\xeb\\x1a\\xba\\xa5\\xfc\\x90\\x88v\\xbe\\r\\x12bE\\x94\\xfe\\xdcI\"T\\x14\\x99%\\x862\\x9b}H\\xb74/\\x8f\\xaf\\xeae\\xe3\\xdb\\xd4\\x0cAc\\xc3\\xd3H\\xd6\\x00o\\xa8\\xf23DbM\\xfd\\x80+,Q\\xb5\\xc6\\xae\\r\\x06{+\\x1b$\\xb4\\xb8\\x8a\\xe0\\xec\\xfb\\xc0^}\\x85=\\xba\\xa6\\xa3L\\x17\\xbd;\\xe2\\xe0$\\xb0:\\x9bi\\x06\\x14\\xe5\\x8a-\\x95+\\nRK\\x97\\xdcz\\xbe\\x18\\x86Z\\xa6%k\\xfb\\x958X\\xa1P\\xf7\\x01\\x06\\xcd\\x02.\\x9bTt\\x8dzkU\\xe5V\\xb8>\\'7\\xf4\\x14%\\xc3W#\\x96\\x8b.\\x1cx\\'\\xae\\xa1\\xb8i&\\x11\\'\\xb5)\\xc2\\x8f\\x84\\x16N\\x8a\\xb5\\nC0!A\\x96y\\xfc\\xa3\\xf0\\x935\\xb2x9U\\x9a\\xe9\\xf9\\xee\\xfa\\xce\\x13\\x9a1\\xd5\\x99\\xf2\\xfd\\xbf\\x80'")
>>> obj
b't\xf8O\xfc\xa5\xa1,\x93\xfeJ\xd5>\xf9\x00\xa9\xd1\xe1`\x15\xfe\x88\x0c\xbf\xc8!\xfe\xcd\x007\x14\x05\xd6\xa5\xbb\nus\x18\x02\x90\xb9\xd3\x89\x03\xe3_@\xf8\xe1\xa3\x87\x0c\xe4)\xb4\xfd\xe7\xb2\xdb\x02~\xeb\x1a\xba\xa5\xfc\x90\x88v\xbe\r\x12bE\x94\xfe\xdcI"T\x14\x99%\x862\x9b}H\xb74/\x8f\xaf\xeae\xe3\xdb\xd4\x0cAc\xc3\xd3H\xd6\x00o\xa8\xf23DbM\xfd\x80+,Q\xb5\xc6\xae\r\x06{+\x1b$\xb4\xb8\x8a\xe0\xec\xfb\xc0^}\x85=\xba\xa6\xa3L\x17\xbd;\xe2\xe0$\xb0:\x9bi\x06\x14\xe5\x8a-\x95+\nRK\x97\xdcz\xbe\x18\x86Z\xa6%k\xfb\x958X\xa1P\xf7\x01\x06\xcd\x02.\x9bTt\x8dzkU\xe5V\xb8>\'7\xf4\x14%\xc3W#\x96\x8b.\x1cx\'\xae\xa1\xb8i&\x11\'\xb5)\xc2\x8f\x84\x16N\x8a\xb5\nC0!A\x96y\xfc\xa3\xf0\x935\xb2x9U\x9a\xe9\xf9\xee\xfa\xce\x13\x9a1\xd5\x99\xf2\xfd\xbf\x80'
>>> obj[0]
116
>>> obj[1]
248
Sign up to request clarification or add additional context in comments.

2 Comments

It kind of worked: the a object iss realy this, and ast turns it into a string not bytes. `'"b\'t\\\\xf8O\\\\xfc\\\\xa5\\\\xa1,........ too long......\\\\n'
@AngusClayto, can you update your question with the string you are trying to transform then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.