1

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.

2
  • can you provide an example of string and it bytea representation(i.e. for example \\xdeadbeaf == hello world)? Commented Jan 9, 2018 at 16:59
  • Example: \x54657374 --> Test Commented Jan 9, 2018 at 17:15

1 Answer 1

2

Ok, following to your example its simple hex representation.

>>> '\\x48414e44203330'.lstrip('\\x').decode('hex')
'HAND 30'
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.