我有一个100MB +这种格式的文本文件。示例小文件为here。
and -0.436527 -0.515304 -0.002056 -0.227969 0.177528 0.201756...
with 0.101336 0.493859 -0.081095 -0.391502 -0.111579 0.388659...
voice -0.168610 0.413912 0.423446 0.484159 -0.546614 0.558571...
尾随数字可以是100位数,包括正数和负数。我根据某个建议使用这段代码进行解析(找到某个文本并对该文本的所有尾随数字求和)。
double[] vectorOne = File.ReadLines(filename)
.Where(line => line.Contains("drop"))
.SelectMany(line => line.Split())
.Where(str => str.All(c => Char.IsDigit(c) || c == '-' || c == '.'))
.Select(str => Double.Parse(str, CultureInfo.InvariantCulture))
.ToArray();
MessageBox.Show( "", vectorOne.Sum().ToString());
但是我收到以下错误:Input string was not in a correct format. at Double.Parse(str, CultureInfo.InvariantCulture)).
非常感谢任何帮助!
答案 0 :(得分:2)
你的问题就在这里:
.SelectMany(line => line.Split())
由于每行末尾都有一个空格,它会为每行提供一个空条目(默认情况下拆分不会删除空条目)
为避免这种情况,您可以这样做:
.SelectMany(line => line.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries))
我用你的output.bin文件试了一下,这次工作正常。
编辑:
double[] vectorOne = File.ReadLines("myOutput.bin")
.Where(line => line.Contains("drop"))
.Select(x => x.Trim())
.SelectMany(line => line.Split())
.Where(str => str.All(c => Char.IsDigit(c) || c == '-' || c == '.'))
.Select(str => Double.Parse(str, CultureInfo.InvariantCulture))
.ToArray();
这也是有用的,你基本上在拆分它之前修剪线,删除最后的空字符