如何逐列将数据写入csv文件? [英] How do I write data column by column to a csv file?

查看:139
本文介绍了如何逐列将数据写入csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





将一些数据写入csv文件时遇到很大问题。我有很多测量值。每个值都由名称,单位,值描述。所以我想为具有这三个属性的列构建每个值。

我想将它存储到csv文件中,如下所示:



宽度CWT粗糙度卷曲指数

mm mm mg / m%

16,2 3,2 0,000 11,7


直到现在我正在编写名称的标题,另一个用于单位和值(以前存储在字符串数组中)我只是在一行中写的。

直到现在我的csv文件看起来像这样:(

宽度; CWT;粗糙度;

mm; mm,粗糙度;卷曲指数;

16,2; 3,2; 0,000; 11,7



如果价值不多我不关心这个但有很多,所以当我打开csv文件时,问题是标题不适合值和单位。它没有组织,我无法将值与标题匹配。

我想要一切都要按列组织。任何帮助都会非常感激!

这就是我现在的代码:

 StreamWriter sw =  new  StreamWriter(  test2.csv ); 
int RowCount = 3 ;
int ColumnCount = 4 ;
string [] [] Tr ansfer_2D = new string [RowCount] [];
Transfer_2D [ 0 ] = new string [ 3 ] { 宽度 CWT Coarseness Curl-Index }; // 值的名称
Transfer_2D [ 1 ] = new string [ 3 ] { mm mm mg / m }; // units
Transfer_2D [ 2 ] = new string [ 3 ] {TransferData [ 0 ],TransferData [ 1 ],TransferData [ 2 ],TransferData [ 3 ]};
for int i = 0 ; i < RowCount; i ++)
{
for (< span class =code-keyword> int j = 0 ; j < ColumnCount ; j ++)
{
sw.Write(Transfer_2D [i] [j]); // 写一行用列分隔
如果(j < ColumnCount)
{
sw.Write( ;); // use;作为列之间的分隔符
}
}
如果(i < ; RowCount)
{
sw.Write( \ n); // 使用\ n分隔行
}
}

sw.Close();

}


>

解决方案

尝试如下

  for  int  i =  0 ; i <  RowCount; i ++) 
{
sw.WriteLine( string join string .Format( {0},Transfer_2D [i]));
}


Hi,

I have a big problem with writing some data to a csv file. I have a lot of measurement values. Every value is described by name, unit, value. So i want to build for every value a column with these three properties.
I want to store it into the csv file like this:

Width CWT Coarseness Curl-Index
mm mm mg/m %
16,2 3,2 0,000 11,7

Till now i was coding a header for the names, another for the units and the values (that were previously stored into a string array) i just wrote in one line.
Till now my csv file looks like this:(
Width;CWT;Coarseness;
mm;mm, Coarseness; Curl-Index;
16,2;3,2;0,000;11,7

if it were not many values i wouldn't care about this but there are a lot so when i open the csv file there's the problem that the headers dont fit to the values and units. It's not organized, i cannot match the values to the headers.
I would like everything to be organized in columns. Any help would be strongly appreciated!
That's the code that i have till now:

StreamWriter sw = new StreamWriter("test2.csv");
        int RowCount = 3;
        int ColumnCount = 4;
string[][] Transfer_2D = new string[RowCount][];
Transfer_2D[0] = new string[3]{"Width", "CWT", "Coarseness", "Curl-Index"}; //name of the values
Transfer_2D[1] = new string[3] {"mm", "mm", "mg/m", "%"};   //units
Transfer_2D[2] = new string[3] { TransferData[0], TransferData[1], TransferData[2], TransferData[3] };
for (int i = 0; i < RowCount; i++)
        {
            for (int j = 0; j < ColumnCount; j++)
            {
                sw.Write(Transfer_2D[i][j]);//write one row separated by columns
                if (j < ColumnCount)
                {
                    sw.Write(";");//use ; as separator between columns
                }
            }
            if (i < RowCount)
            {
                sw.Write("\n");//use \n to separate between rows
            }
        }
        
        sw.Close();

    }


>

解决方案

try like below

for (int i = 0; i < RowCount; i++)
{
   sw.WriteLine(string.join(",", string.Format("'{0}'", Transfer_2D[i]));
}


这篇关于如何逐列将数据写入csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆