juh+ntg-cont...@mailbox.org schrieb am 08.10.2024 um 18:13:
Am 03.10.24 um 14:15 schrieb Wolfgang Schuster:

%%%% begin example
\setuplayout[tight]

\setuppapersize[A8,landscape]

\starttext

\startbuffer[cattle]
"Year","Cattle, total","Dairy cows"
"2009","3968","1489"
"2010","3975","1479"
"2011","3885","1470"
"2012","3879","1484"
"2013","4000","1553"
"2014","4068","1572"
"2015","4134","1622"
"2016","4251","1745"
"2017","4096","1694"
\stopbuffer

% \savebuffer [list=cattle,prefix=no,file=cattle.csv]

\startluacode

local csvsplitter = utilities.parsers.rfc4180splitter()

local tablecontent, tableheader = csvsplitter(buffers.getcontent("cattle"),true) -- local tablecontent, tableheader = csvsplitter(io.loaddata("cattle.csv"),true)

context.bTABLE{ split = "repeat" }
   context.bTABLEhead()
     context.bTR()
       for _, tablecell in next, tableheader do
         context.bTH()
         context(tablecell)
         context.eTH()
       end
     context.eTR()
   context.eTABLEhead()
   context.bTABLEbody()
     for _, tablerow in next, tablecontent do
       context.bTR()
         for _, tablecell in next, tablerow do
           context.bTD()
           context(tablecell)
           context.eTD()
         end
       context.eTR()
     end
   context.eTABLEbody()
context.eTABLE()

\stopluacode

\stoptext
%%%% end example

This works perfectly with commas and doublequotes. Is it possible to configure the splitter so that it works with semicolons as column separator and no char as data separator?

I don't know what you mean with data separator but the quote marks are optional and only needed when the column entry contains the column separator itself. In my example all double quotes except the one for "Cattle, total" are redundant and can be removed.

Something like:

foo;bar;baz
second foo; second bar; second baz


You can change the separator when you create a custom splitter.

local csvsplitter = utilities.parsers.rfc4180splitter{ separator = ";" }

Wolfgang
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to