[Tex/LaTex] using csvreader to read from csv giving invalid output

csvsimplelongtablemactex

I am trying to output a *.csv file to LaTeX, but I keep on getting invalid output. I am using mactex on a yosemite environment.

code.csv:

code,subcd
CITY,LIMA
CITY,PUEBLOLIBRE
CITY,JESUSMARIA
CITY,TARMA

preamble:

\documentclass[10pt]{article}
\usepackage{graphicx,fancyhdr,import,hyperref}
\usepackage{longtable,csvsimple}
\usepackage[margin=.75in]{geometry}
\usepackage[toc,page]{appendix}

using csvreader:

\begin{document}

\section{System Constants}
The following table contains the constants used throughtout the system.

\begin{tabular}{|c|c|}
\csvreader[head to column names]{code.csv}{}{}
\end{tabular}

\end{document}

my output:

output after running pdflatex

I am not sure what I'm doing wrong.
Also, I saw in Importing CSV file as a table in Latex but file too long that one can split a table that is too long into multiple tables with the same header (my table eventually will grow in size and I thought it would be a good idea to account for it). However, after trying the samples in the post above, I seem to get errors.

.tex file:

\section{System Constants}
The following table contains the constants used throughtout the system.

%\csvautotabular{tables/code.csv}
%\begin{tabular}{|c|c|}
%\csvreader[head to column names]{code.csv}{}{}
%\end{tabular}

\csvreader[
    longtable=lrrrr,
    table head=
        \toprule\bfseries Code & \bfseries Sub Code \\
        \midrule\endhead
        \bottomrule\endfoot,
    late after line=\\,
    before reading={\catcode`\#12},
    after reading={\catcode`\#12}
]{code.csv}{}{}

enter image description here

Any ideas on how to use csvsimple with longtable?

Any help would be appreciated!

EDIT:
after some research on csvsimple and pointed on the right direction by Thomas F. Sturm, I found out that I was using the tool incorrectly. The following is an updated version of the .tex code and also note that I've updated the .csv file to subcd

\csvreader[
    longtable=|l|l|,
    table head=
        \toprule
        \bfseries Code & \bfseries Sub Code\\
        \midrule
        \endhead
        \bottomrule,
    table foot=\bottomrule
]{code.csv}{1=\code, 2=\subcd}{\code & \subcd}

However, I seem to get a compilation error on the last line {\code & \subcd}

enter image description here

Best Answer

\documentclass[10pt]{article}
\usepackage{graphicx,fancyhdr,import,hyperref}
\usepackage{longtable}
\usepackage{datatool}
\usepackage[margin=.75in]{geometry}
\usepackage[toc,page]{appendix}


\begin{document}

\section{System Constants}
The following table contains the constants used throughtout the system.

\DTLsetseparator{,}
\DTLloadrawdb[noheader,keys={code,sub_cd}]{ex}{code.csv} %took more robost loaddb
\begin{longtable}%
        {%
        |c|c|
        }%
        A & B \\
        \endfirsthead
        A & B \\
        \endhead
        \multicolumn{2}{r}
                {
                next page
                } \\
        \endfoot
                \multicolumn{2}{r}
                {
                last page
                } \\
        \endlastfoot
\DTLforeach{ex}{\code=code, \subcd=sub_cd}%
            {%
            \DTLiffirstrow{}%
                {%
                    \\%
                }%
             \protect\code&%
             \protect\subcd%
            }%
\end{longtable}

\end{document}

Take datatool. :)

Related Question