如何计算 csv 的行数?

如何计算 csv 的行数?

我正在使用\csvreader它读取 csv,并尝试进行一些基本操作,例如计算值的数量并在文档中打印该值。例如,如何根据以下代码计算男性数量并打印There are a total of \malesnum males

\documentclass{standalone}

\usepackage{csvsimple}


\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}


\begin{document}
    \begin{tabular}{l|c}%
    \bfseries Person & \bfseries Matr.~No.% specify table head
    \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
    {\\\hline\givenname\ \name & \matriculation}% specify your coloumns here
    \end{tabular}
\end{document}

答案1

这是该包的一个示例datatool

\documentclass{article}

\usepackage{datatool}

\newcommand*{\thePassGrade}{2.0}

\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\DTLloaddb{grades}{grade.csv}

\begin{document}

\begin{tabular}{lccc}
Last Name & Matriculation & Gender & Grade \\ \hline
\DTLforeach*[\DTLiseq{\gn}{m}]{grades}%
    {\ln=name,\mat=matriculation,\gn=gender,\gr=grade}
    {%
        \DTLiffirstrow{}{\\}
        \ln & \mat & \gn &  \gr %
    } \\ \hline
\end{tabular}

\DTLsavelastrowcount{\nmales}

\medskip

This database has a total of \nmales{} male students.

\bigskip

\begin{tabular}{lccc}
Last Name & Matriculation & Gender & Grade \\ \hline
\DTLforeach*[\DTLisgt{\gr}{\thePassGrade}]{grades}%
    {\ln=name,\mat=matriculation,\gn=gender,\gr=grade}
    {%
        \DTLiffirstrow{}{\\}
        \ln & \mat & \gn &  \gr %
    } \\ \hline
\end{tabular}

\DTLsavelastrowcount{\pass}

\medskip

This database has a total of \pass{} students who had over \thePassGrade.

\end{document}

答案2

此解决方案使用 pgfplotstable。困难的部分是将两列合并为一列(全名)。

\documentclass{article}

\usepackage{pgfplotstable}

\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}


\begin{document}
    \pgfplotstableread[string type,col sep=comma]{grade.csv}\mytable

    \pgfplotstablegetrowsof{\mytable}%
    number rows = \pgfplotsretval{} (header row doesn't count)

    \pgfplotstablegetcolsof{\mytable}%
    number columns = \pgfplotsretval

    \pgfplotstablecreatecol[create col/assign/.code={%
      \edef\entry{\thisrow{givenname}~\thisrow{name}}
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
    {fullname}\mytable

    \pgfplotstabletypeset[string type,columns={fullname,matriculation},
      every head row/.style={before row=\hline,after row=\hline},
      columns/fullname/.style={column name=\textbf{Person},column type={l|}},
      columns/matriculation/.style={column name=\textbf{Matr.~No.},column type={c}}
      ]{\mytable}

\end{document}

答案3

例如

\begin{filecontents*}{\jobname.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple}

\newcounter{dambocnt}

\begin{document}

\setcounter{dambocnt}{0}
\newcommand{\ismale}{%
  \ifnum\pdfstrcmp{\gender}{m}=0
    \stepcounter{dambocnt}%
  \fi
}

\begin{tabular}{l|c}%
  \bfseries Person & \bfseries Matr.~No.% specify table head
  \csvreader[head to column names]{\jobname.csv}{}% use head of csv as column names
  {\\\hline\ismale\givenname\ \name & \matriculation}% specify your columns here
\end{tabular}

There were \thedambocnt\ males.

\end{document}

相关内容