[Tex/LaTex] Importing csv Table with Underscores

csvsimpleunderscore

I have to import a csv table whose entries contain underscores. A simple example is the following table:

FirstCol, SecondCol
Item_A, Item_B

I am trying to solve my problem with the csvsimple package with the "respect underscore=true" option. Here is a minimal working example:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{array,booktabs}
\usepackage[T1]{fontenc}

\begin{document}

\begin{longtable}{|l|l|}
   \hline
   \csvreader[respect underscore=true, head to column names, 
    late after line=\\\hline]{Test.csv}{}{\FirstCol & \SecondCol}
\end{longtable}

\end{document}

This code produces the following result:

enter image description here

How do I get rid of the obviously wrong column separators just below the table? To be clear, what I would like is this:

enter image description here

Note that the two separators only appear if the "respect underscore=true" option is present. If I remove this option (and delete the underscores from the input file), then all works as expected.

Best Answer

A solution could be pgfplotstable with the verb string type option.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{longtable}
\usepackage{array,booktabs}
\renewcommand*{\arraystretch}{1.2}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}

\usepackage{filecontents}
\begin{filecontents}{Test.csv}
    FirstCol, SecondCol
    Item_A, Item_B
\end{filecontents}

\begin{document}

    \pgfplotstableread[col sep=comma]{Test.csv}{\mytable}

    \pgfplotstabletypeset[
        begin table={\begin{longtable}},
        every head row/.append style={before row={%
                \hline
                \endfirsthead
                \hline
                FirstCol & SecondCol\\
                \hline
                \endhead
            },
        },      
        after row=\hline,       
        end table={\end{longtable}},
        every first column/.style={column type={|l|}},
        every column/.style={column type={l|}},
        verb string type
        ]{\mytable}

\end{document}

enter image description here

Related Question