4

I am trying to print a table from .csv file which is generated by libreoffice in such a way one of the column has a value comma separated e.g John,Terang i want to print John, Terang without any quotes, but it seems to be printed as "John,Terang" because when i open in text editor actual value it shows as "{John,Terang}" so it printed as "John,Terang" how do i print as John,Terang without modifying .csv file Till now i have try so far,

 \documentclass[11pt,a4paper]{article}
 \usepackage{csvsimple}
 \usepackage{filecontents}
 \begin{filecontents*}{file.csv}
 SlNo,Name,Occupation
 1,Daniel Rongpi,"{a,b}"
 2,"{John, Terang}","{service, fire}"
 3,ffdfdf,wewe
 \end{filecontents*}

 \begin{document}

 \begin{tabular}{|l|l|l|}
 \hline
 Sl.No. & Name & Occupation\\\hline
 \csvreader[head to column names]{file.csv}{}
 {\SlNo & \Name & \Occupation\\\hline
 }

 \end{tabular}

\end{document}

The ouput of the above code is shown below enter image description here

but output which i want is something like this without modifying the .csv file

enter image description here

3

You can tell csvsimple to ignore ":

\begin{filecontents*}{\jobname.csv}
SlNo,Name,Occupation
1,Daniel Rongpi,"{a,b}"
2,"{John, Terang}","{service, fire}"
3,ffdfdf,"we we"
\end{filecontents*}


\documentclass[11pt,a4paper]{article}
\usepackage{csvsimple}

\begin{document}

\csvreader[
  tabular=|l|l|l|,
  table head=\hline Sl.No. & Name & Occupation \\\hline,
  late after line=\\\hline,
  head to column names,
  before reading={\catcode`\"=9}
]{\jobname.csv}{}
 {%
  \SlNo & \Name & \Occupation
 }%

\end{document}

enter image description here

  • The magician appears with his TeX tricks :):):) – CarLaTeX Apr 3 '18 at 6:16
  • Thank you so much . I want to add something how do i tell csvsimple to make for grouping i.e whenever it founds " some text, other text" grouping as single block@egreg – Biki Teron Apr 3 '18 at 12:46
  • @BikiTeron I'm afraid this is out of the scope for csvsimple – egreg Apr 3 '18 at 12:52
  • @egreg I have found something from this link en.wikibooks.org/wiki/TeX/catcode but i was unable to apply just like you have used \catcode command. – Biki Teron Apr 3 '18 at 13:14
  • @BikiTeron As far as I can see, csvsimple is not able to cope with " delimited entries for hiding a comma. – egreg Apr 3 '18 at 13:26
3

Instead of using csvsimple, you could use pgfplotstable, which allows you to ignore some chars with ignore chars={"}.

I also advise you to use booktabs for table formatting.

\documentclass[11pt,a4paper]{article}
\usepackage{pgfplotstable}
\usepackage{array, booktabs}
\usepackage{caption}
\renewcommand*{\arraystretch}{1.1}  

\usepackage{filecontents}
\begin{filecontents*}{file.csv}
    SlNo,Name,Occupation
    1,Daniel Rongpi,"{a,b}"
    2,"{John, Terang}","{service, fire}"
    3,ffdfdf,wewe
\end{filecontents*}

\begin{document}

\begin{table}
    \caption{With your table style}
    \centering
    \pgfplotstabletypeset[string type,ignore chars={"},
        col sep=comma,
        columns/SlNo/.style={column type={|l|}, column name={Sl.No.}},
        columns/Name/.style={column type={l|}},
        columns/Occupation/.style={column type={l|}},
        every head row/.style={
            before row=\hline,after row=\hline},
        every last row/.style={
            after row=\hline},
        ]{file.csv}
\end{table}


\begin{table}
    \caption{Or, better, with \texttt{booktabs} style}
    \centering
    \pgfplotstabletypeset[
        string type,ignore chars={"},
        col sep=comma,
        columns/SlNo/.style={column type={l}, column name={Sl.No.}},
        columns/Name/.style={column type={l}},
        columns/Occupation/.style={column type={l}},
        every head row/.style={
            before row=\toprule,after row=\midrule},
        every last row/.style={
            after row=\bottomrule},
        ]
        {file.csv}
\end{table}
\end{document}

enter image description here

enter image description here

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.