Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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}
share|improve this answer
    
I really like how easy the option is to use! Thanks! –  Christoph Aug 9 '11 at 15:06

According to Section 4.2.2 of the pgfplots manual you can add the »skip first« option to a plot command.

\addplot file[skip first] {datafile.dat};
share|improve this answer
    
That seems simpler indeed :) –  Bruno Le Floch Aug 8 '11 at 10:13
    
This is only useful when the file contents are plotted directly, but not if the data is used in a different way or has to be pre-processed. –  Christoph Aug 9 '11 at 15:03
    
Use /pgfplots/table/skip first n={integer i} for \addplot table. Example: \addplot table[skip first n=1, ... –  ComFreek Sep 4 at 19:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.