在有人将此标记为重复问题之前,我已经阅读这答案,但并没有帮助。
我有一个 csv 文件:
"headline","similarity"
"Tesco directors in scandal",""
"Tesco suspends three more senior staff over profits scandal","0.7443551421165466"
"\textsterling11m bonus for Tesco boardroom","0.7385022044181824"
"SSL inquiry claims two boardroom casualties","0.7339619398117065"
"Morrisons executive arrested in insider trading investigation","0.7273239493370056"
"Tesco rubbishes 'chief executive to quit' report","0.7250239849090576"
我正在尝试使用以下方法来读取它:
\begin{table}
\small
\renewcommand{\arraystretch}{1.2}
\sisetup{group-digits=false, table-format=1.3, table-auto-round=true, table-number-alignment = left}
\begin{tabularx}{\columnwidth}{@{}XS@{}} \toprule
{Headline} & {Similarity} \\\midrule
\csvreader[
late after line=\\,
late after last line=\\\midrule,
head to column names,
before reading={\catcode`\"=9}]
{../file.csv}
{}
{\csviffirstrow{\textbf}{} \headline & \similarity}
\end{tabularx}
\end{table}
但这给出了:
我怎样才能删除这里的引号?
我相信我首先需要它们,因为如果它们不在那里,\textsterling
csv 的第 4 行就无法正确解析。
答案1
可以通过在更广泛的范围内设置 catcode(例如在环境中)来解决引号问题table
。这似乎不起作用,因为还有第二个问题,即命令(例如\textsterling
)不允许出现在行首(可能与当行以重音字符开头时,csvsimple 输出奇怪的结果)。当你在开头放置其他东西(例如一个空组)时{}
,它就会按预期工作。
请注意,catcode 字符不需要反斜杠(即\catcode`"=9
有效)。作为一个小细节,您还可以考虑使用`chief executive to quit'
(以反引号开头而不是普通引号)在输出中获得方向引号。
梅威瑟:
\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{siunitx}
\begin{document}
\begin{table}
\catcode`"=9
\small
\renewcommand{\arraystretch}{1.2}
\sisetup{group-digits=false, table-format=1.3, table-auto-round=true, table-number-alignment = left}
\begin{tabularx}{\columnwidth}{@{}XS@{}} \toprule
{Headline} & {Similarity} \\\midrule
\csvreader[
late after line=\\,
late after last line=\\\midrule,
head to column names,
%before reading={\catcode`\"=9},
]
{tesco.csv}
{}
{\csviffirstrow{\textbf}{} \headline & \similarity}
\end{tabularx}
\end{table}
\end{document}
CSV:
"headline","similarity"
"Tesco directors in scandal",""
"Tesco suspends three more senior staff over profits scandal","0.7443551421165466"
"{}\textsterling11m bonus for Tesco boardroom","0.7385022044181824"
"SSL inquiry claims two boardroom casualties","0.7339619398117065"
"Morrisons executive arrested in insider trading investigation","0.7273239493370056"
"Tesco rubbishes `chief executive to quit' report","0.7250239849090576"
结果: