数组函数返回空 [英] array function returning empty
本文介绍了数组函数返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此处的目的是调整此答案,以返回数组而不是 setof 数据类型。
The aim here is to adapt this answer to return array instead of setof datatype.
CREATE FUNCTION split_csvline(
line text, -- the input CSV string
delim_char char(1) = ',', -- can be also E'\t', ';', '|', etc.
quote_char char(1) = '"' -- field quotation
) RETURNS text[] AS $f$
import csv
row = csv.reader(
[line],
quotechar=quote_char,
delimiter=delim_char,
skipinitialspace=True,
escapechar='\\'
)
next(row)
$f$ IMMUTABLE language PLpythonU;
SELECT split_csvline('a,b'); -- empty!
编辑
EDIT
这是一个有关使用Python的问题 PostgreSQL。
It is a question about "using Python with PostgreSQL".
我正在使用 PLpythonU ,因为员工使用Pytho n,并且因为CSV很复杂并且需要可靠(测试年限)算法。
I am using PLpythonU because the staff use Python and because CSV is complex and need reliable (years of test) algorithm.
不需要解决方法,因为使用了一种简单的解决方法:
Not need a workaround, because a simple workaround is in use:
CREATE FUNCTION split_csv_line(
text, char(1) DEFAULT ',', char(1) DEFAULT '"'
) RETURNS text[] AS $f$
SELECT x FROM split_csv($1,$2,$3) x LIMIT 1;
$f$ language SQL IMMUTABLE;
推荐答案
csv.reader
函数返回阅读器
对象。 next
是 reader
对象方法:
The csv.reader
function returns a reader
object. next
is a reader
object method:
create or replace function split_csvline(
_line text,
_delim_char char(1) = ',',
_quote_char char(1) = '"'
) returns text[] as $f$
import csv
reader = csv.reader(
[_line],
quotechar = _quote_char,
delimiter = _delim_char,
skipinitialspace = True,
escapechar = '\\'
)
return reader.next()
$f$ immutable language plpythonu;
select split_csvline('a,b');
split_csvline
---------------
{a,b}
这篇关于数组函数返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文