pgfplotstablesave 和 CSV 文件

pgfplotstablesave 和 CSV 文件

该文件写出一个 CSV 文件:

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{
  1,2
  3,4
  5,6
  7,8
  9,10
}\data
\begin{document}
\pgfplotstablesave[col sep=comma]{\data}{data.csv}
\end{document}

CSV 文件如下所示:

0,1,
1,2,
3,4,
5,6,
7,8,
9,10,

有没有办法避免每行末尾的逗号?

答案1

是的,这是可能的,但根据我的发现,你需要重新定义内部宏。我所做的就是替换

\xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%

\pgfplotstablesave@impl

\ifnum\pgfplotstablecol<\pgfplotstablecols
  \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
\else
  \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka}%
\fi

\pgfplotstablecol是当前列和\pgfplotstablecols列数。仅当 时才会添加逗号\pgfplotstablecol<\pgfplotstablecols。这是首次实施。

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\makeatletter
\def\pgfplotstablesave@impl[#1]#2#3{%
    \pgfplotstabletypeset[%
        reset styles,%
        disable rowcol styles,%
        begin table={},%
        end table={},%
        typeset cell/.code={%
            \begingroup
            \t@pgfplots@toka={##1}%
            \ifcase\pgfplotstableread@OUTCOLSEP@CASE\relax
                % col sep=SPACE:
                \t@pgfplots@tokb=\expandafter{\pgfplotstableread@tab}%
                \pgfplots@ifempty{##1}{%
                    \t@pgfplots@toka={{}}%
                }{}%
                \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
            \or
                % col sep=comma:
                \t@pgfplots@tokb={,}%
                \ifnum\pgfplotstablecol<\pgfplotstablecols
                  \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
                \else
                  \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka}%
                \fi
            \or
                % col sep=semicolon:
                \t@pgfplots@tokb={;}%
                \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
            \or
                % col sep=colon:
                \t@pgfplots@tokb={:}%
                \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
            \or
                % col sep=braces:
                \xdef\pgfplots@glob@TMPc{{\the\t@pgfplots@toka}}%
            \or
                % col sep=tab:
                \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka\pgfplotstableread@tab}%
            \or
                % col sep=&:
                \xdef\pgfplots@glob@TMPc{\the\t@pgfplots@toka&}%
            \fi
            \endgroup
            \pgfkeyslet{/pgfplots/table/@cell content}\pgfplots@glob@TMPc%
        },%
        before row=,%
        after row=,%
        skip coltypes,%
        typeset=false,%
        string type,%
        TeX comment=,%
        columns=,%
        font=,%
        /pgfplots/table/col sep/.is choice,%
        /pgfplots/table/col sep/space/.code     = {\def\pgfplotstableread@OUTCOLSEP@CASE{0}},%
        /pgfplots/table/col sep/comma/.code     = {\def\pgfplotstableread@OUTCOLSEP@CASE{1}},%
        /pgfplots/table/col sep/semicolon/.code = {\def\pgfplotstableread@OUTCOLSEP@CASE{2}},%
        /pgfplots/table/col sep/colon/.code     = {\def\pgfplotstableread@OUTCOLSEP@CASE{3}},%
        /pgfplots/table/col sep/braces/.code    = {\def\pgfplotstableread@OUTCOLSEP@CASE{4}},%
        /pgfplots/table/col sep/tab/.code       = {\def\pgfplotstableread@OUTCOLSEP@CASE{5}},%
        /pgfplots/table/col sep/&/.code         = {\def\pgfplotstableread@OUTCOLSEP@CASE{6}},%
        /pgfplots/table/col sep/ampersand/.code = {\def\pgfplotstableread@OUTCOLSEP@CASE{6}},%
        /pgfplots/table/col sep=space,%
        /pgfplots/table/in col sep/.is choice,%
        /pgfplots/table/in col sep/space/.code      = {\def\pgfplotstableread@COLSEP@CASE{0}},%
        /pgfplots/table/in col sep/comma/.code      = {\def\pgfplotstableread@COLSEP@CASE{1}},%
        /pgfplots/table/in col sep/semicolon/.code  = {\def\pgfplotstableread@COLSEP@CASE{2}},%
        /pgfplots/table/in col sep/colon/.code      = {\def\pgfplotstableread@COLSEP@CASE{3}},%
        /pgfplots/table/in col sep/braces/.code     = {\def\pgfplotstableread@COLSEP@CASE{4}},%
        /pgfplots/table/in col sep/tab/.code        = {\def\pgfplotstableread@COLSEP@CASE{5}},%
        /pgfplots/table/in col sep/&/.code          = {\def\pgfplotstableread@COLSEP@CASE{6}},%
        /pgfplots/table/in col sep/ampersand/.code  = {\def\pgfplotstableread@COLSEP@CASE{6}},%
        /pgfplots/table/in col sep=space,%
        % WARNING: you NEED a '%' before '#1':
        #1,%
        /pgfplots/table/include outfiles=false,
        /pgfplots/table/outfile={#3}%
    ]{#2}%
}%
\makeatother
\pgfplotstableread[col sep=comma]{
  1,2
  3,4
  5,6
  7,8
  9,10
}\data
\begin{document}
\pgfplotstablesave[col sep=comma,after row={}]{\data}{data.csv}

Hello
\end{document}

结果是

0,1
1,2
3,4
5,6
7,8
9,10

目前我只对逗号情况做了这个,但很容易将它应用于所有列分隔符。但我觉得应该提出一个功能请求(如果没有内置解决方案)。添加一个仅在用户同意的情况下启用此修改的键(即使我觉得这应该是默认的)可能更安全,以免破坏旧代码。

相关内容