将多维数组保存到CSV文件 [英] Save multidimensional array to CSV file
本文介绍了将多维数组保存到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将大型多维数组保存到CSV文件的最佳方法是什么?
例如,
double [,] data =新的double [10000,650],每个单元格都填充有数据.
我想可视化10000行x 650列矩阵
What is the best way to save a large multidimensional array to a CSV file?
For example,
double[,] data = new double[10000,650] and each cell is populated with data.
I would like to visualize a 10000 row x 650 column matrix
推荐答案
此处的代码将您的数组保存到csv中,并可能向您展示如何实现:
This code here saves your array to a csv and may show you how you could achieve it:
private void Form1_Load(object sender, EventArgs e)
{
double[,] data = new double[10000, 650];
using (StreamWriter outfile = new StreamWriter(@"C:\Temp\test.csv"))
{
for (int x = 0; x < 10000; x++)
{
string content = "";
for (int y = 0; y < 650; y++)
{
content += data[x, y].ToString("0.00") + ";";
}
outfile.WriteLine(content);
}
}
}
在这里,我可以看到将多维数组转换为CSV文件的更可行,更高效的代码.请检查此链接,
到CSV的多维数组 [ ^ ]
链接向您显示了字符串数组中的CSV,但希望您可以将此函数转换为双精度数组.
[更新]
更新的解决方案:这是我创建的.这样可能更有效.
Hi,
Here i can see more feasible and efficient code for converting Multidimensional array into CSV file. Please check this link,
Multidimensional Array to CSV[^]
Link show you CSV from string array but hope you can convert this function into double array.
[Update]
Updated Solution : This is what i have created. This may be more efficient.
string path = "mycsv.csv";
double[,] multiDimensionalArray = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 } };
var enumerator = multiDimensionalArray.Cast<double>()
.Select((s, i) => (i + 1) % 3 == 0 ? string.Concat(s, Environment.NewLine) : string.Concat(s, ","));
var item = String.Join("", enumerator.ToArray<string>());
File.AppendAllText(path, item);
[/更新]
谢谢
-Amit Gajjar
[/Update]
Thanks
-Amit Gajjar
用Environment.NewLine
分隔行,保证行内的单元格值不保证值(制表符,空格,句点,...)中不存在任何字符.
Separate the rows by an Environment.NewLine
, the cell values within a row by whatever character is guaranteed not to exist within values (tab, space, period, ...).
这篇关于将多维数组保存到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文