在Python中读取PostgreSQL数组数据

在Python中读取PostgreSQL数组数据,python,python-3.x,postgresql,psycopg2,Python,Python 3.x,Postgresql,Psycopg2,在使用Python的psycopg2进行查询之后 SELECT id, array_agg(еnty_pub_uuid) AS ptr_entity_public FROM table GROUP BY id 我得到一个数组: {a630e0a3-c544-11ea-9b8c-b73c488956ba,c2f03d24-2402-11eb-ab91-3f8e49eb63e7} 如何用python将其解析为列表 psycopg2中是否有内置函数?您需要为python和po

在使用Python的psycopg2进行查询之后

SELECT 
    id,
    array_agg(еnty_pub_uuid) AS ptr_entity_public 
FROM table
GROUP BY id
我得到一个数组:

{a630e0a3-c544-11ea-9b8c-b73c488956ba,c2f03d24-2402-11eb-ab91-3f8e49eb63e7} 
如何用python将其解析为列表


psycopg2中是否有内置函数?

您需要为python和postgres注册UUID类型以推断类型


导入psycopg2.extras
psycopg2.extras.register_uuid()
sql=”“”
挑选
身份证件
数组聚合(发布ID)作为ptr实体公共
从桌子上
按id分组
"""
cursor=con.cursor()
cursor.execute(sql)
结果=cursor.fetchall()
对于结果中的r:
打印(类型(r[1]))

psycopg2
关心python和postgres之间的类型对话:

import psycopg2

conn = psycopg2.connect("...")
cur = conn.cursor()
cur.execute(
    "select user_id, array_agg(data_name) from user_circles where user_id = '81' group by user_id"
)
res = cur.fetchall()
print(res[0])
print(type(res[0][1]))
输出:

('81',['f085b2e3-b943-429e-850f-4ecf358abcbc','65546d63-be96-4711-a4c1-a09f48fbb7f0','81d03c53-9d71-4b18-90c9-d33322b0d3c6','00000000-0000-0000-00000000')

查询中还有其他参数。问题是如何在python中读取此数组?
('81', ['f085b2e3-b943-429e-850f-4ecf358abcbc', '65546d63-be96-4711-a4c1-a09f48fbb7f0', '81d03c53-9d71-4b18-90c9-d33322b0d3c6', '00000000-0000-0000-0000-000000000000'])
<class 'list'>