1

I use Python 2.7 to write data into MySQL blob column:

data_list=[{ id': 0, 'binarydata': 
'\x04\x0f\x002\x00\x00\x00\x00\x00\x03\x00 \x00\x06\x00@\x00\t\x00` 
\x00\x0c\x00\x80\x00\x0f\x0’}]  

cursor.executemany(SQL, data_list)

I want to read binarydata back from database in the same format as I put above:

I tried:

SELECT id, HEX(binarydata) as binarydata FROM T;  

I got:

  data_list=[{ id': 0, 'binarydata': u'040F00320000000000030020000600400009..'}]  

The difference is: binarydata now has prefix u' and all \x which were present in original data_list are gone.
Question: how to get data in original format (with \x and without prefix u')?

1 Answer 1

1

In Python2.7 there is a builtin library binascii.

By using binascii.hexlify you can take bytearray e.g. \x04\x0f and convert it to hex value. By using binascii.unhexlify you can do it other way around.

import binascii

a = b'\x04\x0f'
b = binascii.hexlify(a)

print b  # '040f'
print binascii.unhexlify(b)  # '\x04\x0f'
Sign up to request clarification or add additional context in comments.

Comments

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.