简体   繁体   中英

Convert SQL blob/bytea in dump to string in Python

I have a pg_dump of a table that contains a column of type bytea. It is a long string like: \\\\x4e696365206d7573696361...

Normally in SQL I would use a simple:

SELECT user_id,
       encode(text_column::bytea, 'escape')
FROM posts
LIMIT 10

And that provides the original text.

How can I convert this in Python? I've been trying .encode / .decode / base64encode etc but no luck.

Ok, following to your example its simple hex representation.

>>> '\\x48414e44203330'.lstrip('\\x').decode('hex')
'HAND 30'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM