Python - Querying bitcoin's block header using leveldb I am facing a stupid problem. I am trying to read bitcoin/blocks/index leveldb using plyvel. I've set up a regtest and i know there is a block with the hash :

27ce8199dee22f96e4f392dec29ef207fcd8a3c9e43b7d58cb40a5ba01af674b


I am trying to query it using plyvel.

Note that I already read the topics :

How does Bitcoin read from/write to LevelDB

What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?

https://en.bitcoin.it/wiki/Protocol_documentation

Then, I know I have to swap the endianness so it becomes :

MYVAR = "4b67af01baa540cb587d3be4c9a3d8fc07f29ec2de92f3e4962fe2de9981ce27"


Thus, I should query something like :

b + (byte) MYVAR 


However I do not get how. I guess I missunderstand something in encoding. I tried :

db.get(b'b4b67af01baa540cb587d3be4c9a3d8fc07f29ec2de92f3e4962fe2de9981ce27')
db.get(b'b'+b'4b67af01baa540cb587d3be4c9a3d8fc07f29ec2de92f3e4962fe2de9981ce27')


..

Then, I opened the bitcoin/blocks/index leveldb using a gui explorer and I noticed the key for this block is :

\x62\x4b\x67\xaf\x01\xba\xa5\x40\xcb\x58\x7d\x3b\xe4\xc9\xa3\xd8\xfc\x07\xf2\x9e\xc2\xde\x92\xf3\xe4\x96\x2f\xe2\xde\x99\x81\xce\x27


which is \x62 corresponding to the "b" + the hash with \x (escaping caracter ? I know here is my misunderstanding on hex encoding)

and obviously 

db.get(b'\x62\x4b\x67\xaf\x01\xba\xa5\x40\xcb\x58\x7d\x3b\xe4\xc9\xa3\xd8\xfc\x07\xf2\x9e\xc2\xde\x92\xf3\xe4\x96\x2f\xe2\xde\x99\x81\xce\x27')


perfectly works

I also tried these methods :

https://stackoverflow.com/questions/5649407/hexadecimal-string-to-byte-array-in-python

No one of them works and I really don't get why :

bytes.fromhex(hash)
bytearray.fromhex(hash)


Thank you in advance
The leveldb databases are serialized for optimization and do not work well with attempting to manually parse the data. You can more easily get details on a specific blockhash using the built-in JSON-RPC API and use the getblock call.