Search code examples
pythonpostgresqlcsvplpython

array function returning empty


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

Notes

It is a question about "using Python with PostgreSQL".

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;

Solution

  • 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}