0

So...

I have to pull data from postgres database using python ant return the results (JSON formatted) to a web page via an AJAX call

I have to live with a nasty constraint in that I have no access to python SQL libraries (it's a black box appliance with limited libraries) so I have to shell out to OS and parse the results.

A PITA but no real problem so far

Today's problem though is with returned Postgres array types

A typical value returned as a string would be, and yes, some of the elements do contain colons..

{foo,"bar bar",baz:qux,999}

Ideally I'd like to convert to

'values':['foo','bar bar","baz:qux","999"]

Any good ideas or do I need to spend some time on regex101.com

0

2 Answers 2

0

Something like this:

In [24]: strs="""{foo,"bar bar",baz:qux,999}"""

In [27]: [x.strip('"{}') for x in strs.split(",")]
Out[27]: ['foo', 'bar bar', 'baz:qux', '999']
2
  • Looking good, but spitting out the string from the OS read gives me strs='{foo,"bar bar",baz:qux,999}'. The strip and split combo leaves me with ['foo', '"bar bar', 'baz:qux', '999'] - see the feral double-quote in the 2nd element Commented May 1, 2013 at 3:14
  • @PerryW this example works fine for me, I am not getting any double quotes. Commented May 1, 2013 at 3:52
0

I'd use a pure-python library like pure-python library like py-postgresql from http://python.projects.pgfoundry.org/ that doesn't require any additional native code, personally.

If necessary you could even hack it to include it in-line in a single script, rather than as multiple files.

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.