2

I have merge 3 tables from code in link: merge 3 csv file and create new file

Table E = Merge of 3 Tables (A,B &D)

But how i can change cell in Table E = 0 with blank? If cell in Table A, B, D is zero, then this cell in Table E (after Merge) = blank.

Minimal coding:

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{datatool}

\begin{filecontents*}{A.csv}
date,colA
2016-01-01,0
2016-01-02,4
2016-01-03,2
\end{filecontents*}

\begin{filecontents*}{B.csv}
date,colB0,colB1,colB2,colB3
2016-01-01,0,b10,b11,b12
2016-01-02,0,0,b11,b12
2016-01-03,4,b20,b21,b22
\end{filecontents*}

\begin{filecontents*}{D.csv}
date,colD0,colD1,colD2,colD3
2016-01-01,0,d10,d11,d12
2016-01-03,4,d20,d21,d22
\end{filecontents*}

\DTLloaddb{E}{A.csv}
\DTLloaddb{B}{B.csv}
\DTLloaddb{D}{D.csv}

% Specific request
% Now, i want to create new file E with columns:
% Date colA colB0 colB1 colB2 colD0 colD1 colD2
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colB0}{B}{date}{\Date}\DTLappendtorow{colB0}{\DTLifnull{\tmp}{}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colB1}{B}{date}{\Date}\DTLappendtorow{colB1}{\DTLifnull{\tmp}{}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colB2}{B}{date}{\Date}\DTLappendtorow{colB2}{\DTLifnull{\tmp}{}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colD0}{D}{date}{\Date}\DTLappendtorow{colD0}{\DTLifnull{\tmp}{}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colD1}{D}{date}{\Date}\DTLappendtorow{colD1}{\DTLifnull{\tmp}{}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colD2}{D}{date}{\Date}\DTLappendtorow{colD2}{\DTLifnull{\tmp}{}{\tmp}}}

\begin{document}
Table E 
\DTLsetseparator{,}
\DTLsetdelimiter{"}
\DTLsavedb{E}{E.csv} % doesn't write if before \begin{document}
\DTLdisplaydb{E}
\end{document}

In image: enter image description here

Thank you

1 Answer 1

2

Define a test for the entry being zero.

\documentclass[border=4]{standalone}
\usepackage{datatool}
\usepackage{pdftexcmds}

\begin{filecontents*}{A.csv}
date,colA
2016-01-01,0
2016-01-02,4
2016-01-03,2
\end{filecontents*}

\begin{filecontents*}{B.csv}
date,colB0,colB1,colB2,colB3
2016-01-01,0,b10,b11,b12
2016-01-02,0,0,b11,b12
2016-01-03,4,b20,b21,b22
\end{filecontents*}

\begin{filecontents*}{D.csv}
date,colD0,colD1,colD2,colD3
2016-01-01,0,d10,d11,d12
2016-01-03,4,d20,d21,d22
\end{filecontents*}

\DTLloaddb{E}{A.csv}
\DTLloaddb{B}{B.csv}
\DTLloaddb{D}{D.csv}

\makeatletter
\newcommand{\xDTLifzero}[1]{%
  \ifnum\pdf@strcmp{0}{#1}=\z@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

% Specific request
% Now, I want to create new file E with columns:
% Date colA colB0 colB1 colB2 colD0 colD1 colD2
\DTLforeach{E}{\Date=date}{%
  \DTLgetvalueforkey{\tmp}{colA}{E}{date}{\Date}%
  \DTLreplaceentryforrow{colA}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
%
  \DTLgetvalueforkey{\tmp}{colB0}{B}{date}{\Date}%
  \DTLappendtorow{colB0}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
%
  \DTLgetvalueforkey{\tmp}{colB1}{B}{date}{\Date}%
  \DTLappendtorow{colB1}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
%
  \DTLgetvalueforkey{\tmp}{colB2}{B}{date}{\Date}%
  \DTLappendtorow{colB2}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
%
  \DTLgetvalueforkey{\tmp}{colD0}{D}{date}{\Date}%
  \DTLappendtorow{colD0}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
%
  \DTLgetvalueforkey{\tmp}{colD1}{D}{date}{\Date}%
  \DTLappendtorow{colD1}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
%
  \DTLgetvalueforkey{\tmp}{colD2}{D}{date}{\Date}%
  \DTLappendtorow{colD2}{\DTLifnull{\tmp}{}{\xDTLifzero{\tmp}{}{\tmp}}}%
}

\begin{document}
Table E 
\DTLsetseparator{,}
\DTLsetdelimiter{"}
\DTLsavedb{E}{E.csv} % doesn't write if before \begin{document}
\DTLdisplaydb{E}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .