4

I have a CSV file, using "@" as the delimiter, containing data such as this:

 Code     @ Title        @ Page     @ Tags                    @ File
 001      @ Buying Tips  @ 1        @ seafood, fish, octopus  @ 1.pdf
 001      @ Buying Tips  @ 2        @ oranges, apples         @ 2.pdf
 001      @ Buying Tips  @ 3        @ bananas                 @ 3.pdf
 002      @ Food Waste   @ 1        @ compost, apples         @ 4.pdf
 002      @ Food Waste   @ 2        @ plastic, paper, bags    @ 5.pdf

I need to call specific data from the CSV file, either displaying information found in a specific line or displaying the file that is list in the last column.

Examples might be:

  • Display page 1 of code 002 (would show 4.pdf).
  • Display all pages with the tag "apples" (would show 2.pdf and 4.pdf).
  • Display just the 2nd occuring item in the CSV that has the tag "apples" (would show 4.pdf).
  • Display the title of page 2, code 1 (would show "Buying Tips").

Does ConTeXt have any method by which to call data in such a way from a CSV file?

2
  • Wouldn't »Display page 1 of code 002« show 4.pdf? I don't think I understand your input format. Jul 9, 2017 at 23:11
  • Edited to fix error.
    – Village
    Jul 9, 2017 at 23:15

1 Answer 1

5

You can parse the contents of a CSV file with ConTeXt's built-in rfc4180splitter. The examples you give could then be easily achieved using some Lua code. Here I show the usage of the parser to get you started. There are many more parsers found in util-prs.lua, you should definitely have a look.

\startbuffer[csv]
Code     @ Title        @ Page     @ Tags                    @ File
001      @ Buying Tips  @ 1        @ seafood, fish, octopus  @ 1.pdf
001      @ Buying Tips  @ 2        @ oranges, apples         @ 2.pdf
001      @ Buying Tips  @ 3        @ bananas                 @ 3.pdf
002      @ Food Waste   @ 1        @ compost, apples         @ 4.pdf
002      @ Food Waste   @ 2        @ plastic, paper, bags    @ 5.pdf
\stopbuffer

\startluacode
local csvsplitter = utilities.parsers.rfc4180splitter{separator = "@"}
local rows, colnames = csvsplitter(buffers.getcontent"csv", true)

-- Show what has been parsed on the terminal
inspect(rows)
inspect(colnames)

-- Now pick data and process to your liking
-- ...
\stopluacode

\starttext

\stoptext

On the terminal you'll see

table={
 { "001      ", " Buying Tips  ", " 1        ", " seafood, fish, octopus  ", " 1.pdf" },
 { "001      ", " Buying Tips  ", " 2        ", " oranges, apples         ", " 2.pdf" },
 { "001      ", " Buying Tips  ", " 3        ", " bananas                 ", " 3.pdf" },
 { "002      ", " Food Waste   ", " 1        ", " compost, apples         ", " 4.pdf" },
 { "002      ", " Food Waste   ", " 2        ", " plastic, paper, bags    ", " 5.pdf" },
}
table={
 "Code     ",
 " Title        ",
 " Page     ",
 " Tags                    ",
 " File",
}
1
  • 1
    In util-prs.lua there is also csvsplitter but this cannot split off the head from the content. That is why I use rfc4180splitter instead. Jul 9, 2017 at 23:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .