6

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.

  • You can't have entries split across lines, sorry. – egreg Apr 2 '15 at 14:38
7

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.

  • I like your answer better. I will delete mine. – Steven B. Segletes Apr 2 '15 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.