1

我从 plpgsql 得到一个如下所示的数组:

[0:2]={
"(ab023500-ceef-41d6-af33-635964dbffde,Koen,\"\",Schmeets,{koen@heizoo.com},31631205427},\"{\"\"Test categorie\"\"}\",{ff0000})",
"(384cb1e9-58b9-4bdf-9da6-eb3d9355471b,Marc,\"\",Vrijhof,{},{},\"{\"\"Test categorie\"\"}\",{ff0000})",
"(9c19ec5c-6b95-456a-af6f-c3388835b780,Michael,\"\",\"Baas ;)\",{},{},    \"{\"\"Subcategorie test\"\",\"\"Test categorie\"\"}\",\"{NULL,ff0000}\")"}

我已经构建了自己的解释器来获取 Python 列表中的数组,但它似乎不是防故障的。有人知道如何在 Python 中解析这个数组吗?

编辑

unnest()使我可以使用以下功能获得一个不错的数组:

CREATE FUNCTION array_to_json_string(in_varchararray character varying[])
  RETURNS character varying LANGUAGE plpythonu AS
$_$
import cjson
   
plan = plpy.prepare("SELECT * FROM unnest($1)", ["varchar[]"])
rv = plpy.execute(plan, [in_varchararray])

retList = []
for r in rv:
    retList .append(r["unnest"])
return cjson.encode(retList)
$_$;

虽然......它很慢!

有人知道如何将 plpgsql 数组转换为逗号分隔值吗?

4

2 回答 2

0

考虑在数组进入 python 之前取消嵌套,或者在 8.4 中使用unnest或者使用你自己的:

create or replace function unnest(anyarray) returns setof anyelement as $$
  select $1[i] from generate_series(array_lower($1,1), array_upper($1,1)) i;
$$ language'sql' immutable;
于 2010-12-13T18:33:32.470 回答
0

用于array_to_string()将数组转换为逗号分隔的字符串。
像这样:

SELECT array_to_string(
    $$[0:2]={
    "(ab023500-ceef-41d6-af33-635964dbffde,Koen,\"\",Schmeets,{koen@heizoo.com},{31631205427},\"{\"\"Test categorie\"\"}\",{ff0000})",
    "(384cb1e9-58b9-4bdf-9da6-eb3d9355471b,Marc,\"\",Vrijhof,{},{},\"{\"\"Test categorie\"\"}\",{ff0000})",
    "(9c19ec5c-6b95-456a-af6f-c3388835b780,Michael,\"\",\"Baas ;)\",{},{},\"{\"\"Subcategorie test\"\",\"\"Test categorie\"\"}\",\"{NULL,ff0000}\")"
    }$$::text[]
    , ',');
于 2012-03-01T01:32:40.707 回答