我正在尝试从 libreoffice 生成的 .csv 文件中打印一个表格,其中一列的值用逗号分隔,例如John,Terang
我想打印时John, Terang
不带任何引号,但它似乎被打印为,"John,Terang"
因为当我在文本编辑器中打开实际值时它显示为"{John,Terang}"
所以它打印为"John,Terang"
我如何在John,Terang
不修改 .csv 文件的情况下打印到现在我已经尝试过了,
\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}
但我想要的输出是这样的,无需修改.csv 文件
答案1
你可以告诉csvsimple
忽略"
:
\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}
答案2
除了使用csvsimple
,您还可以使用pgfplotstable
,这允许您忽略一些字符ignore chars={"}
。
我还建议您使用booktabs
表格格式。
\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}