separatedlist with header repeating processing csv to table
Dear all, some years ago I asked about how to process a csv file in a table with repeating headers on following pages, but got no answer. https://www.mail-archive.com/ntg-context@ntg.nl/msg98903.html Now I am again working on this problem. I wonder if there is a better way to process csv files to a table with more flexibility? Or do I simply miss the switch to mark the first row in the csv file as the header that has to repeat? TIA juh
Am 02.10.24 um 09:48 schrieb juh+ntg-context--- via ntg-context:
Dear all,
some years ago I asked about how to process a csv file in a table with repeating headers on following pages, but got no answer.
https://www.mail-archive.com/ntg-context@ntg.nl/msg98903.html
Now I am again working on this problem.
I wonder if there is a better way to process csv files to a table with more flexibility?
Or do I simply miss the switch to mark the first row in the csv file as the header that has to repeat?
Did you try split=repeat instead of split=yes? Hraban
Am 02.10.24 um 13:49 schrieb Henning Hraban Ramm:
Am 02.10.24 um 09:48 schrieb juh+ntg-context--- via ntg-context:
Dear all,
some years ago I asked about how to process a csv file in a table with repeating headers on following pages, but got no answer.
https://www.mail-archive.com/ntg-context@ntg.nl/msg98903.html
Now I am again working on this problem.
I wonder if there is a better way to process csv files to a table with more flexibility?
Or do I simply miss the switch to mark the first row in the csv file as the header that has to repeat?
Did you try split=repeat instead of split=yes?
Oh, and of course you need a \bTABLEhead, otherwise there’s nothing to repeat. That means AFAIK, the header can’t be part of the CSV, using this simple module. Hraban
Am 02.10.24 um 14:29 schrieb Henning Hraban Ramm:
Oh, and of course you need a \bTABLEhead, otherwise there’s nothing to repeat. That means AFAIK, the header can’t be part of the CSV, using this simple module.
I tried to put the header into the before part of setupseparatedlist, but this does not work. I also fear that I cannot do it with this module. Are there other means to build a table from csv? juh
On 03/10/2024 10:49, juh+ntg-context--- via ntg-context wrote:
I tried to put the header into the before part of setupseparatedlist, but this does not work. I also fear that I cannot do it with this module. Are there other means to build a table from csv?
have you checked the handlecsv module?
juh+ntg-context--- via ntg-context schrieb am 03.10.2024 um 10:49:
Am 02.10.24 um 14:29 schrieb Henning Hraban Ramm:
Oh, and of course you need a \bTABLEhead, otherwise there’s nothing to repeat. That means AFAIK, the header can’t be part of the CSV, using this simple module.
I tried to put the header into the before part of setupseparatedlist, but this does not work. I also fear that I cannot do it with this module. Are there other means to build a table from csv?
ConTeXt has a better parser for csv files since many years (which is used by the already mentioned handlecsv module) but there is no TeX interface for it. The example below shows how the parser can be used to store csv data in a Lua table, you can even split of the first line of data to use it as header etc. Afterwards you can use a loop to create a TeX table out of the stored Lua table. %%%% 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 Wolfgang
vm via ntg-context schrieb am 03.10.2024 um 16:33:
On 03/10/2024 14:15, Wolfgang Schuster wrote:
Afterwards you can use a loop to create a TeX table out of the stored Lua table.
just the freshup the mind, how do you include a \setupTABLE in the lua section? like \setupTABLE[r][first][background=color, backgroundcolor=yellow]
You write it as context.setupTABLE({"r"}, {"first"}, {background="color", backgroundcolor="yellow"}) When there is only a single argument with brackets you can omit () but not in this case where the command takes multiple argument. For more information about TeX commands in Lua take a look at the "ConTeXt Lua document" (cld-mkiv.pdf) manual. Wolfgang
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? Something like: foo;bar;baz second foo; second bar; second baz TIA juh
Am 08.10.24 um 18:13 schrieb juh+ntg-context--- via ntg-context:
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?
Something like:
foo;bar;baz second foo; second bar; second baz
Use the source, juh! In https://source.contextgarden.net/tex/context/base/mkiv/util-prs.lua?search=r... you can find that rfc4180splitter takes a "specification" parameter that looks like { separator = ",", quote = '"' }. I didn’t check if it works without quote character. Hraban
juh+ntg-context@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
Am 08.10.24 um 19:16 schrieb Wolfgang Schuster:
You can change the separator when you create a custom splitter.
local csvsplitter = utilities.parsers.rfc4180splitter{ separator = ";" }
Thanks a lot. Just curious. I often wonder in pure ConTeXt when to use curly bracets and when square brackets In this lua code I would think we would need simple rounded ones. Like in your first example where you call the splitter without options: local csvsplitter = utilities.parsers.rfc4180splitter() I am sure that this is some lua convention, but, nevertheless, it puzzles me. Ciao! juh
juh+ntg-context@mailbox.org schrieb am 08.10.2024 um 20:10:
Am 08.10.24 um 19:16 schrieb Wolfgang Schuster:
You can change the separator when you create a custom splitter.
local csvsplitter = utilities.parsers.rfc4180splitter{ separator = ";" }
Thanks a lot.
Just curious. I often wonder in pure ConTeXt when to use curly bracets and when square brackets
In this lua code I would think we would need simple rounded ones.
Like in your first example where you call the splitter without options:
local csvsplitter = utilities.parsers.rfc4180splitter()
I am sure that this is some lua convention, but, nevertheless, it puzzles me.
It's explained in the Lua manual: %%%% begin quotation Lua 5.4 Reference manual [...] 3.4.10 -- Function calls [...] A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. [...] %%%% end quotation Wolfgang
participants (4)
-
Henning Hraban Ramm
-
juh+ntg-context@mailbox.org
-
vm
-
Wolfgang Schuster