简体   繁体   English

将字符串数组转换为多维double

[英]Converting string array to multidimensional double

I need to read a matrix from a file in to double[,]. 我需要从文件中读取一个矩阵以double [,]。 Till now it is possible to read the file as a string[]. 到现在为止,可以将文件读取为string []。 The datastructure looks like that: 数据结构如下所示: 在此处输入图片说明

But the conversion doesn't work. 但是转换不起作用。

I already tried a lot of stackoverflow articles, but it failed always with "Input string was not in a correct format". 我已经尝试了很多stackoverflow文章,但是总是失败,因为“输入字符串的格式不正确”。 Important is that the values are all double values and seperated with three spaces. 重要的是这些值都是双精度值,并用三个空格分隔。

I read the file like this: 我这样读取文件:

var text = File.ReadAllLines(@"right.txt");

You can do this several ways, and it depends if you want a jagged array, or a multidimensional array. 您可以通过多种方式执行此操作,这取决于您是否需要锯齿状数组或多维数组。

There are probably more succinct ways of doing this, however, you can see the idea. 可能有更简洁的方法,但是,您可以看到这个想法。

Jagged 锯齿状

var jaggedArray = File.ReadAllLines(@"d:\right.txt")
                        .Where(x => !string.IsNullOrWhiteSpace(x))
                        .Select(x => x.Split(new[]{' '},StringSplitOptions.RemoveEmptyEntries)
                                     .Select(double.Parse)
                                     .ToArray())
                        .ToArray();

Multidimensional 多维的

var lines = File.ReadAllLines(@"d:\right.txt")
                  .Where(x => !string.IsNullOrWhiteSpace(x))
                  .Select(x => x.Split(new[]{' '},StringSplitOptions.RemoveEmptyEntries)
                               .Select(double.Parse)
                               .ToList())
                  .ToList();

var h = lines.Count();
var w = lines.Max(x => x.Count);
var multiArray = new double[h, w];

for (var i = 0; i < lines.Count; i++)
   for (var j = 0; j < lines[i].Count; j++)
      multiArray[i, j] = lines[i][j];

Note : Totally untested, and I am not responsible for the people maim or otherwise harm with this code 注意 :完全未经测试,对于此代码对人们造成的伤害或其他伤害,我不承担任何责任
Note 2 : There is no error checking or fault tolerance, if there is garbage in your file, this will likely throw 注意2 :没有错误检查或容错功能,如果文件中有垃圾,这很可能会抛出

Update 更新资料

Is there a way to debug/fix that? 有没有办法调试/修复该问题? Maybe the problem is that the double values are separated with 3 spaces and not only 1 space? 可能是双精度值用3个空格而不是仅1个空格分隔的问题? Is there a way to fix this in a LINQ statement? 有没有办法在LINQ语句中解决此问题?

Yes if there are multiple spaces using StringSplitOptions.RemoveEmptyEntries should work 是,如果使用StringSplitOptions.RemoveEmptyEntries可以使用多个空格,则可以

Split(new [] {' '},StringSplitOptions.RemoveEmptyEntries)

However note, this really assumes, that every separated value can be converted to a double , and there is no blank lines (even at the end of the file) 但是请注意,这实际上是假设每个分隔的值都可以转换为double ,并且没有空行(即使在文件末尾)

You can solve the blank line problem by using something like this included in the linq statement 您可以使用linq语句中包含的类似内容来解决空白行问题

File.ReadAllLines(@"right.txt")
    .Where(x => !string.IsNullOrWhiteSpace(x))
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM