1

i read topic: Merge two .csv files and add missing rows? And i want change some things when i merge 3 different files

This minimal working example:

 \documentclass{article}
\usepackage{filecontents}
\usepackage{datatool}

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

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

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


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

\DTLforeach{E}{\Date=date}{
    \DTLgetvalueforkey{\tmp}{colB0}{B}{date}{\Date}
    %\DTLgetvalueforkey{\tmp}{colB1}{B}{date}{\Date}
    \DTLappendtorow{colB0}{\DTLifnull{\tmp}{0}{\tmp}}
}

\begin{document}

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

\DTLdisplaydb{E}

\end{document}

Now, i want to create new file E with columns:

Date colA colB0 colB1 colB2 colD0 colD1 colD2

Please use DTLgetvalueforkey & Datatool.

Thank in advance

2

Here is the PDF output

enter image description here

And here is the CSV file built by the code below

enter image description here

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

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

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

\begin{filecontents*}{D.csv}
date,colD0,colD1,colD2,colD3
2016-01-01,2,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}{0}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colB1}{B}{date}{\Date}\DTLappendtorow{colB1}{\DTLifnull{\tmp}{0}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colB2}{B}{date}{\Date}\DTLappendtorow{colB2}{\DTLifnull{\tmp}{0}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colD0}{D}{date}{\Date}\DTLappendtorow{colD0}{\DTLifnull{\tmp}{0}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colD1}{D}{date}{\Date}\DTLappendtorow{colD1}{\DTLifnull{\tmp}{0}{\tmp}}}
\DTLforeach{E}{\Date=date}{\DTLgetvalueforkey{\tmp}{colD2}{D}{date}{\Date}\DTLappendtorow{colD2}{\DTLifnull{\tmp}{0}{\tmp}}}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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