![无法使用 csvsimple 将 csv 文件数据导入到 latex 表中](https://linux22.com/image/369195/%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8%20csvsimple%20%E5%B0%86%20csv%20%E6%96%87%E4%BB%B6%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%85%A5%E5%88%B0%20latex%20%E8%A1%A8%E4%B8%AD.png)
我不知道为什么数据行没有显示出来。只有标题,这是我的 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*
只是为了使示例自成一体。