[Tex/LaTex] skipping the first line of a data file for pgfplotstable

datatoolfilesystem-accesspgfplotstable

I need to skip the first row of a csv file, which is auto-generated by some magic measurement apparatus. It contains its own filename as the first line. The second line contains the column names (no problem) and then the data follows:

C:\someFolder\measure\....\measurement.csv
"F_1k", ....
134.56, ....

I have found the method of reading the whole file line by line and then writing every line but the first one to a temporary file, but that seems brutal. TH.'s answer to File input and output can be modified to do just that.

As far as I know, pgfplots and datatool can not skip lines of a csv file.

Manually removing the first line is not an option, I have several hundreds of these beasts. I also try to avoid external programs because the resulting code will be used on different platforms.

Best Answer

The code below adds a key to \DTLloaddb, which I called omitlines (please propose a better name): it is an integer which tells us how many lines to omit from the beginning of the file. Removing the lines is done through a \loop...\repeat construction, before the datatool really starts parsing the file (but after that file is opened).

The filecontents package is not part of the solution, but is useful to produce external files from a TeX file (and have a self-contained MWE).

\documentclass{article}
\usepackage{datatool}

% The idea is to add a key to \DTLloaddb which gives
% the number of lines that should be skipped. Then
% use etoolbox to patch the internal command \\@dtlloaddb
% which does the file loading, and add a loop at the right
% place to discard some lines.
\usepackage{etoolbox}
\makeatletter
\newcount{\dtl@omitlines}
\define@key{loaddb}{omitlines}{\dtl@omitlines=#1\relax}
\expandafter\patchcmd\expandafter{\csname\string\@dtlloaddb\endcsname}
  {\DTLnewdb}
  {%
    \loop
    \ifnum \dtl@omitlines > \z@
      \advance\dtl@omitlines by \m@ne
      \read\@dtl@read to \@dtl@line
    \repeat
    \DTLnewdb
  }
  {\PackageInfo{datahack}{Sucess!}}
  {\PackageError{datahack}{Failure!}{}}
\makeatother

% Produce the csv file.
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
JUNK!
More junk!
He,ade,rs
1,2,3
4,5,6
\end{filecontents*}
\begin{document}
  % Load the db.
  \DTLloaddb[omitlines=2]{mydata}{\jobname.csv}
  % Display it.
  \DTLdisplaydb{mydata}
\end{document}
Related Question