![pgfplotstablesave 和 CSV 文件](https://rvso.com/image/405339/pgfplotstablesave%20%E5%92%8C%20CSV%20%E6%96%87%E4%BB%B6.png)
該檔案寫出一個 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
目前我只針對逗號情況這樣做,但很容易將其應用於所有列分隔符號。但我覺得應該提出一個功能請求(如果沒有內建解決方案)。添加一個僅在用戶這麼說時才打開此修改的密鑰可能也更安全(儘管我認為這應該是默認值),以免破壞舊代碼。