0

I use pgfplotstable to read in external .csv data and print them in a formatted (MWE is unformatted) way.

The issue is, that the input .csv is not completely under my control. This means, it could be, that the input data are corrupted. A typically example would be, that the row length is unequal within these data.

So my question is, is there a way to test such a scenario?

MWE

\documentclass[10pt,oneside,a4paper]{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\setlength\parindent{0pt}

% This example if fine
\begin{filecontents}{data1.csv}
    A;B;C  
    D;E;F 
    G;H;I 
\end{filecontents}

% This example is wrong
\begin{filecontents}{data2.csv}
    A,B;C  % => "," instead of a ";"
    D;E;F 
    G;H;I 
\end{filecontents}

\newcommand{\checkInutData}[1]{
    % If input data is fine, do
        \pgfplotstableread[col sep=semicolon]{#1}{\csvdata}
        \pgfplotstableset{verb string type}
        \pgfplotstabletypeset{\csvdata}
    % Else
        % Print, that input data are incorrect
}

\begin{document}
    % This works!
    \checkInutData{data1.csv}\\\\
    % This should print an error
    %\checkInutData{data2.csv}      
\end{document}
4
  • in your other question, you already showed how to count the items, so you could e.g. count them and compare with whatever the number should be. Commented Aug 1, 2024 at 12:51
  • @cfr But the problem occurs while read in the data. So I have no chance to test anything. Commented Aug 1, 2024 at 13:13
  • You could consider doing the check outside of LaTeX before you start, for example with awk: stackoverflow.com/questions/33050427/…. With a little bit of trouble you could also call such a oneliner from within your LaTeX code and proceed based on the return value, but it is easier to make sure the file is ok before you run LaTeX. Commented Aug 2, 2024 at 13:39
  • @Marijn Yes in general you are completely right. But there will be situations, where I can't ensure the correctness of these data. The code is compiled within a CI/CD environment. So I don't have immediately access to this machine. Of course I could also bring up some services in the CI/CD to check this correctness, but this will make it even more complicated. I have requested a feature request to \pgfplotstable to just get a return value if the import was successful or not. I think this could be helpful also for others. Commented Aug 2, 2024 at 20:44

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.