无法使用 csvsimple 将 csv 文件数据导入到 latex 表中

无法使用 csvsimple 将 csv 文件数据导入到 latex 表中

我不知道为什么数据行没有显示出来。只有标题,这是我的 csv 文件:

method,name,error,occurences
socket,socket,"ConnectionResetError(54, 'Connection reset by peer')",12
socket,socket,"CryptoError('Decryption failed. Ciphertext failed verification',)",60
socket,socket,"SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:748)')",77
socket,socket,"ConnectionRefusedError(61, 'Connection refused')",992
socket,socket,"BrokenPipeError(32, 'Broken pipe')",3

我的 MWE:

\begin{document}
\usepackage{csvsimple}
\usepackage{datatool}  
\begin{table}[h]
    \centering
    \caption[Table~\ref{fig:socketdropconnerr} Error during socket test]{Error during socket test}
    \vspace{0.2cm}    
    \label{fig:drop-error-socket}
    \begin{tabular}{ l l }%
        \toprule error & occurences \\
        \csvreader[head to column names]{csv/dropped-conn/error-socket.csv}{}%
        {\\\hline\error & \occurences}%
        \\\hline
    \end{tabular}
\end{table}
\end{document}

答案1

问题似乎出在错误列的内容上。该包似乎无法理解周围"s 中包含的内容是一个单元格。结果是它忽略了无法解析的行,并且由于所有行都是如此,因此每行都会被忽略。

不要使用"来表示包含逗号的单元格,而要使用花括号。另外,下划线在这里不起作用,您必须使用 来转义它\。以下方法有效:

CSV:

method,name,error,occurences
socket,socket,{ConnectionResetError(54, 'Connection reset by peer')},12
socket,socket,{CryptoError('Decryption failed. Ciphertext failed verification',)},60
socket,socket,{SSLEOFError(8, 'EOF occurred in violation of protocol (\_ssl.c:748)')},77
socket,socket,{ConnectionRefusedError(61, 'Connection refused')},992
socket,socket,{BrokenPipeError(32, 'Broken pipe')},3

特克斯:

\documentclass{article}
\usepackage{csvsimple}
\usepackage{datatool}  
\usepackage{booktabs}

\begin{document}
\begin{table}[h]
    \centering
    \caption[Table~\ref{fig:socketdropconnerr} Error during socket test]{Error during socket test}
    \vspace{0.2cm}    
    \label{fig:drop-error-socket}
    \begin{tabular}{ l l }%
        \toprule error & occurences \\
        \midrule
        \csvreader[late after line=\\,head to column
        names]{data3.csv}{}%
        {\error & \occurences}%
        \bottomrule
    \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案2

如果 CSV 文件的条目包含逗号,最好使用不同的分隔符。

\begin{filecontents*}{\jobname.csv}
method;name;error;occurences
socket;socket;ConnectionResetError(54, 'Connection reset by peer');12
socket;socket;CryptoError('Decryption failed. Ciphertext failed verification',);60
socket;socket;SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:748)');77
socket;socket;ConnectionRefusedError(61, 'Connection refused');992
socket;socket;BrokenPipeError(32, 'Broken pipe');3
\end{filecontents*}

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{csvsimple}

\begin{document}

\begin{table}[htp]
\centering
\caption{Error during socket test}
\label{fig:drop-error-socket}

\vspace{0.2cm}    

\csvreader[
  head to column names,
  separator=semicolon,
  tabular=ll,
  table head=\toprule error & occurrences \\ \midrule,
  table foot=\bottomrule,
  respect underscore,
]{\jobname.csv}{}
{\error & \occurences}

\end{table}

\end{document}

另请参阅respect underscore避免问题的选项_

我以前filecontents*只是为了使示例自成一体。

在此处输入图片描述

相关内容