Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
up vote 0 down vote accepted

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']
share|improve this answer
    
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 – PerryW May 1 '13 at 3:14
    
@PerryW this example works fine for me, I am not getting any double quotes. – Ashwini Chaudhary May 1 '13 at 3:52

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.