将 csv 文件导入具有唯一第一列的表格

将 csv 文件导入具有唯一第一列的表格

尝试从 csv 文件创建一个表格,其中我可以手动或通过向量/列定义第一列。

如果第一列中唯一的不同之处是从 1 到 12 的下标,则它有效,如下所示:

\begin{table}[]
    \centering
  \resizebox{\textwidth}{!}{  
    \begin{tabular}{c?c|c|c|c|c|c|c|c|c|c|c|c}
        \textbf{Mode} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} & \textbf{11} & \textbf{12} \\\PARTYLINE
        %
        \csvreader[column count=12,no head,late after line=\\\hline]{Results/E1_EVx.csv}{1=\aaa, 2=\bbb, 3=\ccc, 4=\ddd, 5=\eee, 6=\fff, 7=\ggg, 8=\hhh, 9=\iii, 10=\jjj, 11=\kkk, 12=\lll}
        {\textbf{DOF$_\thecsvrow$} & \aaa & \bbb & \ccc & \ddd & \eee & \fff & \ggg & \hhh & \iii & \jjj & \kkk & \lll}
    \end{tabular}}
\end{table}

在此处输入图片描述

但如果我希望第一行是例如 [DOF A, Dof B, Dof C, Dof D, Dof E, Dof F, Dof G, Dof H, Dof I, Dof J, Dof K, Dof L]

有谁知道我该如何做到这一点,而无需编辑包含数据的 csv 文件。

我得到了这个用我们的值从 matlab 生成的 csv 文件

0.19,0.62,1,0.95,0,0,0,0,-0.06,-0.05,-0.03,-0.04
-0.08,-0.18,-0.1,0.18,-0.16,-0.9,-1,0.65,-0.36,-0.93,1,0.59
-0.08,-0.18,-0.1,0.18,0.16,0.9,1,-0.65,-0.36,-0.93,1,0.59
0.52,0.95,0.07,-1,0,0,0,0,0.05,-0.08,0.09,-0.02
-0.08,0.06,0.4,0.06,0.35,1,-0.33,1,0.62,0.78,0.55,1
-0.08,0.06,0.4,0.06,-0.35,-1,0.33,-1,0.62,0.78,0.55,1
0.81,0.17,-0.94,0.73,0,0,0,0,-0.03,0.13,0.07,0.02
-0.06,0.28,-0.11,-0.24,-0.61,-0.2,0.89,0.89,-0.81,0.17,-0.76,1
-0.06,0.28,-0.11,-0.24,0.61,0.2,-0.89,-0.89,-0.81,0.17,-0.76,1
1,-1,0.73,-0.36,0,0,0,0,-0.07,0.01,-0.03,0.04
-0.04,0.26,-0.48,0.45,1,-0.78,0.63,0.38,1,-1,-0.91,0.6
-0.04,0.26,-0.48,0.45,-1,0.78,-0.63,-0.38,1,-1,-0.91,0.6

答案1

另一种方法是使用knitr,但使用booktabs最简洁优雅的规则和siunitx数字格式(检查连字符是否显示为减号而不是简单的连字符!)。请注意,R 代码通过可选参数和控制平板电脑的尺寸scale(例如,最好保留一个小字体)。sizedigitsdigits=1

姆韦

微波辐射计(Rstudio)或 MWE.Rtex(Overleaf):

\documentclass[11pt]{article}
\usepackage{booktabs,tabulary,siunitx,array}
\usepackage{lipsum}

\begin{filecontents*}[overwrite]{test.csv}
0.19,0.62,1,0.95,0,0,0,0,-0.06,-0.05,-0.03,-0.04
-0.08,-0.18,-0.1,0.18,-0.16,-0.9,-1,0.65,-0.36,-0.93,1,0.59
-0.08,-0.18,-0.1,0.18,0.16,0.9,1,-0.65,-0.36,-0.93,1,0.59
0.52,0.95,0.07,-1,0,0,0,0,0.05,-0.08,0.09,-0.02
-0.08,0.06,0.4,0.06,0.35,1,-0.33,1,0.62,0.78,0.55,1
-0.08,0.06,0.4,0.06,-0.35,-1,0.33,-1,0.62,0.78,0.55,1
0.81,0.17,-0.94,0.73,0,0,0,0,-0.03,0.13,0.07,0.02
-0.06,0.28,-0.11,-0.24,-0.61,-0.2,0.89,0.89,-0.81,0.17,-0.76,1
-0.06,0.28,-0.11,-0.24,0.61,0.2,-0.89,-0.89,-0.81,0.17,-0.76,1
1,-1,0.73,-0.36,0,0,0,0,-0.07,0.01,-0.03,0.04
-0.04,0.26,-0.48,0.45,1,-0.78,0.63,0.38,1,-1,-0.91,0.6
-0.04,0.26,-0.48,0.45,-1,0.78,-0.63,-0.38,1,-1,-0.91,0.6
\end{filecontents*}

\begin{document}  
\lipsum[1][1-4] % to see the actual margins
\tabcolsep1pt
<<results='asis',echo=F,message=F,warning=F>>=
require(xtable)
bold <- function(x){paste0('{\\bfseries ', x, '}')}
df <- data.frame(Mode=paste("DOF ", LETTERS[1:12], sep="")) # Mode data
df[,2:13] <- read.csv("test.csv",header=F) # read the number of external file
colnames(df) <- c("Mode",1:12) # Names fir columns 
rownames(df) <- df[,1]  # "Mode" as rownames (no header)
df[,1] <- NULL          # "Mode" as rownames (no header)
tdf <- xtable(df, caption="My small table.", align=c("p{5em}",rep("S[table-format = 3.2]",12)), digits=2)
print(tdf,  booktabs=T, size="\\scriptsize",
      include.rownames=T,
      sanitize.rownames.function = bold,
      sanitize.colnames.function = bold, 
      scale=1,
      caption.placement = "top")
@
\end{document}

答案2

我从未使用过csvreader,但正如评论中提到的,我的解决方案是pgfplotstable

桌子

\documentclass{standalone}

\usepackage{pgfplotstable}

\begin{document}
    \pgfplotstabletypeset[
        col sep=comma, fixed,
        %create on use/Mode/.style={create col/set list={1,2,...,12}},
        %create on use/Mode/.style={create col/set list={a,b,...,l}},
        create on use/Mode/.style={create col/set list={A,B,...,L}},
        columns/Mode/.style={ % write as subscript
            string type,
            preproc cell content/.code={
                \pgfkeysgetvalue{/pgfplots/table/@cell content}\oldcontent%
                \edef\temp{\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{DOF$_{\oldcontent}$}}%
                \temp%
            }
        },
        columns={Mode,0,1,2,3,4,5,6,7,8,9,10,11},
        assign column name/.code={ % header bold and fix start of index
            \ifnum\pgfplotstablecol>0
                \pgfkeyssetvalue{/pgfplots/table/column name}{\pgfmathparse{int(#1+1)}\textbf{\pgfmathresult}}%
            \else
                \pgfkeyssetvalue{/pgfplots/table/column name}{\textbf{#1}}%
            \fi
        },
        every column/.style={column type/.add={|}{}},
        every first column/.style={column type/.add={}{|}},
        every last column/.style={column type/.add={}{|}},
        every nth row={1}{before row=\hline},       
        every head row/.style={before row=\hline, after row=\hline\hline},
        every last row/.style={after row=\hline}
    ]{E1_EVx.csv}
\end{document}

答案3

让我们用 使事情变得简单csvsimple

A

使用类似这样的命令定义新名称\CellValue

\documentclass[11pt]{article}

\usepackage{csvsimple-l3}   

\usepackage{graphicx}

\begin{filecontents*}[overwrite]{E1EVx.csv}
0.19,0.62,1,0.95,0,0,0,0,-0.06,-0.05,-0.03,-0.04
-0.08,-0.18,-0.1,0.18,-0.16,-0.9,-1,0.65,-0.36,-0.93,1,0.59
-0.08,-0.18,-0.1,0.18,0.16,0.9,1,-0.65,-0.36,-0.93,1,0.59
0.52,0.95,0.07,-1,0,0,0,0,0.05,-0.08,0.09,-0.02
-0.08,0.06,0.4,0.06,0.35,1,-0.33,1,0.62,0.78,0.55,1
-0.08,0.06,0.4,0.06,-0.35,-1,0.33,-1,0.62,0.78,0.55,1
0.81,0.17,-0.94,0.73,0,0,0,0,-0.03,0.13,0.07,0.02
-0.06,0.28,-0.11,-0.24,-0.61,-0.2,0.89,0.89,-0.81,0.17,-0.76,1
-0.06,0.28,-0.11,-0.24,0.61,0.2,-0.89,-0.89,-0.81,0.17,-0.76,1
1,-1,0.73,-0.36,0,0,0,0,-0.07,0.01,-0.03,0.04
-0.04,0.26,-0.48,0.45,1,-0.78,0.63,0.38,1,-1,-0.91,0.6
-0.04,0.26,-0.48,0.45,-1,0.78,-0.63,-0.38,1,-1,-0.91,0.6
\end{filecontents*}
    
\newcommand{\CellValue}{% added <<<<<<<<<<<<<<<<
\ifnum \thecsvrow =1 DOF A \fi
\ifnum \thecsvrow =2 DOF B \fi
\ifnum \thecsvrow =3 DOF C \fi
\ifnum \thecsvrow =4 DOF D \fi
\ifnum \thecsvrow =5 DOF E \fi
\ifnum \thecsvrow =6 DOF F \fi
\ifnum \thecsvrow =7 DOF G \fi
\ifnum \thecsvrow =8 DOF H \fi
\ifnum \thecsvrow =9 DOF I \fi
\ifnum \thecsvrow =10 DOF J \fi
\ifnum \thecsvrow =11 DOF K \fi
\ifnum \thecsvrow =12 DOF L \fi
}
    
\begin{document}        

\begin{table}
    \centering
    \resizebox{\textwidth}{!}{%<<< important
        \begin{tabular}{l|c|c|c|c|c|c|c|c|c|c|c|c}
            \textbf{Mode} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} & \textbf{11} & \textbf{12} \\ 
            \hline
            \csvreader[column count=12,no head,late after line=\\\hline]{E1EVx.csv}{1=\aaa, 2=\bbb, 3=\ccc, 4=\ddd, 5=\eee, 6=\fff, 7=\ggg, 8=\hhh, 9=\iii, 10=\jjj, 11=\kkk, 12=\lll}
            {\textbf{\CellValue} & \aaa & \bbb & \ccc & \ddd & \eee & \fff & \ggg & \hhh & \iii & \jjj & \kkk & \lll}
    \end{tabular}}
\end{table}

\end{document}

保证金说明

这种方式\CellValue虽然容易理解,但效率很低:\ifnum每次都要执行全部 12 个步骤。执行时间会随着条目数的平方而增长。

更好的方法是实现一次查找以立即找到正确的值。执行时间将随着条目数的增加而线性增加。

首先,通过使用 为每个条目创建一个新命令来填充查找\lookupPut{<row number>}{<cell value>}

单元格的值通过以下方式获取\CellValue {<row number>}

如果某一行没有定义单元格值,则该单元格将填充“?????”,如图所示。

d

实现查找

\documentclass[11pt]{article}

\usepackage{csvsimple-l3}   

\usepackage{graphicx}

\begin{filecontents*}[overwrite]{E1EVx.csv}
0.19,0.62,1,0.95,0,0,0,0,-0.06,-0.05,-0.03,-0.04
-0.08,-0.18,-0.1,0.18,-0.16,-0.9,-1,0.65,-0.36,-0.93,1,0.59
-0.08,-0.18,-0.1,0.18,0.16,0.9,1,-0.65,-0.36,-0.93,1,0.59
0.52,0.95,0.07,-1,0,0,0,0,0.05,-0.08,0.09,-0.02
-0.08,0.06,0.4,0.06,0.35,1,-0.33,1,0.62,0.78,0.55,1
-0.08,0.06,0.4,0.06,-0.35,-1,0.33,-1,0.62,0.78,0.55,1
0.81,0.17,-0.94,0.73,0,0,0,0,-0.03,0.13,0.07,0.02
-0.06,0.28,-0.11,-0.24,-0.61,-0.2,0.89,0.89,-0.81,0.17,-0.76,1
-0.06,0.28,-0.11,-0.24,0.61,0.2,-0.89,-0.89,-0.81,0.17,-0.76,1
1,-1,0.73,-0.36,0,0,0,0,-0.07,0.01,-0.03,0.04
-0.04,0.26,-0.48,0.45,1,-0.78,0.63,0.38,1,-1,-0.91,0.6
-0.04,0.26,-0.48,0.45,-1,0.78,-0.63,-0.38,1,-1,-0.91,0.6
\end{filecontents*}
    


%% https://tex.stackexchange.com/a/249529/161015
%% A way to implement a lookup is using the hash table of TeX by defining
% a macro \lookup@<key> which expands to <value>:   
\makeatletter
\newcommand{\CellValue}[1]{%
    \@ifundefined{lookup@#1}
    {?????}{\@nameuse{lookup@#1}}%
}
\newcommand{\lookupPut}[2]{%
    \@namedef{lookup@#1}{#2}%
}
\makeatother

        
\begin{document}
        
\lookupPut{1}{DOF A}
\lookupPut{2}{DOF B}
\lookupPut{3}{DOF C}    
\lookupPut{4}{DOF D}
\lookupPut{5}{DOF E}
\lookupPut{6}{DOF F}
\lookupPut{7}{DOF G}
\lookupPut{8}{DOF H}
\lookupPut{9}{DOF I}    
\lookupPut{10}{DOF J}
\lookupPut{11}{DOF K}
%\lookupPut{12}{DOF L}  % deliberately not defined  

\begin{table}
    \centering
    \resizebox{\textwidth}{!}{%<<< important
        \begin{tabular}{l|c|c|c|c|c|c|c|c|c|c|c|c}
            \textbf{Mode} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} & \textbf{11} & \textbf{12} \\ 
            \hline
            \csvreader[column count=12,no head,late after line=\\\hline]{E1EVx.csv}{1=\aaa, 2=\bbb, 3=\ccc, 4=\ddd, 5=\eee, 6=\fff, 7=\ggg, 8=\hhh, 9=\iii, 10=\jjj, 11=\kkk, 12=\lll}
            {\textbf{\CellValue{\thecsvrow}} & \aaa & \bbb & \ccc & \ddd & \eee & \fff & \ggg & \hhh & \iii & \jjj & \kkk & \lll}
    \end{tabular}}
\end{table}
    

相关内容