[NTG-context] CSV scanners built in ConTeXt - feature or bug?

Hans Hagen pragma at wxs.nl
Thu Feb 26 10:47:29 CET 2015


On 2/26/2015 1:40 AM, Jaroslav Hajtmar wrote:
> \starttext
>
> \startluacode
> local mycsvsplitter = utilities.parsers.rfc4180splitter{
>      separator = ",",
>      quote = '"',
> }
>
> local crap = io.loaddata("data.txt")
>
> -- with header variant
> local tablerows, columnname = mycsvsplitter(crap,true)
> inspect(tablerows)
> inspect(columnname)
>
> -- without header variant
> -- local tablerows = mycsvsplitter(crap)
> -- inspect(tablerows)
>
> for i=1,#tablerows do
>      local l = tablerows[i]
>       for j=1,#l do context(l[j]..", ")
>      end
>      context('\\crlf')
> end
>
> \stopluacode
>
>
> \stoptext

line 527 in util-prs.lua:

     local wholeblob   = Ct((newline^(specification.strict and -1 or 1) 
* record)^0)

should do the trick

i'm not sure about the default as the standard might demand quit at 
empty line so that needs to be figured out (not by me therefore by you)

\starttext

\startluacode
local crap = [[
1,"2","3","4"
"a","b","c","d"
"foo","bar""baz","boogie","xyzzy"
"    ","    ","    ","     "
"And now","followed by","several","blank lines"


1,"2","3","4"
"a","b","c","d"
"foo","bar""baz","boogie","xyzzy"
"    ","    ","    ","     "
]]

local mycsvsplitter = {
     utilities.parsers.rfc4180splitter{
         separator = ",",
         quote = '"',
         strict = true,
     },
     utilities.parsers.rfc4180splitter{
         separator = ",",
         quote = '"',
     }
}

for i=1,#mycsvsplitter do
     local tablerows, columnname = mycsvsplitter[i](crap,true)
     context.formatted.title("Case %s",i)
     for i=1,#tablerows do
         local l = tablerows[i]
          for j=1,#l do context(l[j]..", ")
         end
         context('\\crlf')
     end
end
\stopluacode

\stoptext


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list