convert pgsql int array into python array
<p>I have numeric data (int) stored in pgsql as arrays. These are <code>x,y,w,h</code> for rectangles in an image e.g. <code>{(248,579),(1,85)}</code></p>

<p>When reading to my python code (using psycopg) I get it as a string (?). I am now trying to find the best way to obtain a python array of ints from that string. 
Is there a better way than to split the string on ',' and so on...</p>

<p>p.s. I did try <code>.astype(int)</code> construct but that wouldn't work in this instance.</p>

<p>If you mean the rectangle is stored in Postgresql as an array of points:</p>

<pre><code>query = '''
    select array[(r[1])[0],(r[1])[1],(r[2])[0],(r[2])[1]]::int[]
    from (values ('{"(248,579)","(1,85)"}'::point[])) s (r)
'''

cursor.execute(query)
print cursor.fetchone()[0]
</code></pre>

<p>Returns a Python int list</p>

<pre><code>[248, 579, 1, 85]
</code></pre>

<p>Notice that while a Postgresql array is base 1 by default, a <code>Point</code> coordinate is retrieved as a base 0 array.</p>

python arrays postgresql psycopg2