使用 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中有内置函数吗?
最佳答案
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-0000-000000000000'])
<class 'list'>
关于python - 在Python中读取PostgreSQL数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67430109/