[Tex/LaTex] How to deal with csv file whoese fields are enclosed by quote

csvcsvsimple

I have a csv file whose fields contain text. As fields can contains newlines, they are enclosed by double quotes. Editing these file works well with my favourite spreadsheet (libreoffice). I want to render this file but I got the following issues:

  • the quotes are rendered
  • the new lines are ignored

I use csvsimple, but it may not be the best choice. Here is a simple working sample in 3 files:

data.csv:

"1st field", "numerical field", "string field"
"foo", 3, "bar"
"xizzy", 0, "lorem
ipsum"
"nothing", 1,"last string"

data_simple.csv:

1st field, numerical field, string field
foo, 3, bar
xizzy, 0, lorem ipsum
nothing, 1,last string

csvtest.tex:

\documentclass{article}
\usepackage{geometry}
\usepackage[french]{babel}
\usepackage[utf8]{inputenc}
%\usepackage{pgfplotstable}
\usepackage{csvsimple}

\begin{document}
\csvautotabular{./data_simple.csv}
\csvautotabular{./data.csv}
\end{document}

the table corresponding to data_simple.csv is OK, the one corresponding to data.csv is not as I expected.

Best Answer

TeX can be instructed to ignore a character, by assigning it the category code 9. This will not affect usages of \" in the file.

\begin{filecontents*}{\jobname.csv}
"1st field", "numerical field", "string field"
"foo", 3, "bar"
"xizzy", 0, "lorem
ipsum"
"nothing", 1,"last string"
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

\begingroup\catcode`"=9
\csvautotabular{\jobname.csv}
\endgroup

\end{document}

I used filecontents* with \jobname.csv as name just for avoiding clobbering my files.

enter image description here

Note, however, that you can't have an entry straddling two lines. This is due to the fact that cvssimple reads the file line by line with TeX's method and there is no way for it to decide a line has not ended, with that markup.