我已合并链接中代码中的 3 个表:合并 3 个 csv 文件并创建新文件
表 E = 合并 3 个表 (A、B 和 D)
但是我如何才能将表 E = 0 中的单元格更改为空白?如果表 A、B、D 中的单元格为零,则表 E 中的这个单元格(合并后)=空白。
最少编码:
\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}
谢谢
答案1
定义一个条目为零的测试。
\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}