将文本文件读入二维双数组

Read text file into 2D double array

我有以下文本文件。

289,112,287,112,286,110,285,110,282,109,280,109,278,109,275,108,273,108,271,106,269,106,266,106,264,106,261,105,258,105,257,105,255,105,254,104,253,104,251,104,249,104,248,104,245,104,244,104,240,104,238,104,235,104,232,104,228,106,226,106,222,107,221,108,216,111,214,111,211,114,209,115,205,118,201,121,198,123,195,126,191,129,189,131,185,135,184,136,180,140,178,143,176,147,174,151,171,155,171,157,168,162,167,165,166,170,166,174,165,179,165,185,165,188,165,192,165,195,165,198,165,201,168,204,169,207,172,211,176,216,177,219,181,223,185,226,188,230,193,234,198,237,204,241,210,244,216,247,223,249,230,252,237,254,244,256,251,257,260,258,268,260,276,260,283,262,290,262,296,262,300,262,306,262,312,262,315,262,319,262,321,260,326,258,330,254,333,251,337,244,339,241,342,235,344,230,345,225,348,219,346,179,342,173,340,170,337,165,336,163,334,161,332,159,330,156,328,154,326,153,325,151,323,150,321,147,320,146,319,145,316,143,314,142,311,140,310,139,308,139,307,137,305,136,304,135,302,135,302,134,300,133,300,132,298,130,297,130,297,129,295,128,295,127,294,127,294,126,293,125,292,125,292,124,291,123,290,122,289,121,288,120,287,120,287,119,286,119
254,116,253,116,252,116,251,116,250,116,247,116,244,117,238,120,234,120,229,123,223,126,217,131,210,137,204,141,198,145,193,149,191,152,187,155,182,159,181,161,179,164,178,166,177,170,176,172,175,178,174,182,174,186,173,189,173,195,173,200,173,204,173,208,173,214,173,220,177,224,180,230,184,234,189,239,192,242,197,244,205,247,211,249,217,252,227,254,235,257,242,258,251,259,258,259,267,259,274,259,281,259,286,259,292,259,296,259,301,258,307,255,312,251,318,246,323,238,330,231,334,226,336,220,339,213,340,209,340,205,341,198,341,191,341,184,341,178,341,171,341,166,340,162,338,158,337,156,336,152,334,151,334,149,331,147,330,146,328,143,326,142,325,141,323,140,320,138,318,138,317,137,315,136,311,135,309,134,307,133,306,132,303,132,302,131,299,131,298,130,295,130,292,129,290,127,287,127,286,127,285,127,283,126,282,126,281,125,280,125,279,125,278,125,277,125,276,125,274,124,272,124,271,123,269,123,267,123,265,123,264,123,263,123,261,122,260,122,259,122,258,122,258,121,257,121,257,120
301,143,300,143,299,143,297,143,294,141,291,140,289,139,286,139,282,139,277,136,272,135,268,135,262,134,258,134,252,132,248,132,244,132,239,132,234,132,228,132,222,132,218,132,211,132,206,132,199,132,195,133,188,136,184,137,180,139,177,140,173,142,171,143,169,145,167,147,166,151,163,155,162,158,159,165,157,172,155,178,155,185,155,192,155,200,155,206,155,214,156,220,160,227,166,236,171,243,176,250,184,257,190,263,197,269,207,275,214,280,223,284,230,287,240,290,249,292,257,294,265,294,274,295,279,295,286,295,292,295,300,294,305,292,312,289,318,287,326,282,333,277,340,271,345,266,351,261,356,258,360,253,364,249,365,246,366,243,369,239,369,236,369,232,369,228,369,224,369,221,369,217,369,212,368,209,367,208,366,204,364,200,363,198,360,194,359,191,357,187,356,185,354,182,352,180,351,178,350,177,348,173,347,173,346,172,344,172,342,169,341,169,339,169,337,168,335,166,334,166,330,165,329,164,327,163,325,162,323,161,320,159,318,159,316,158,314,158,313,157,311,157,311,156,310,155,309,155,307,155,307,154,306,154,305,153,304,153,303,152,303,151,302,151,301,151,300,150,299,148,298,148,297,147

我想将上述文件保存到基于彗差分隔的二维双数组中。 例如:

array[0][0]=289...
array[1][0]=254...
array[2][0]=301...

在上面的文件中,每一行都有不同的列长度。

首先,逐行读取整个文件,以便将每一行作为一个单独的字符串:

var lines = System.IO.File.ReadAllLines("Input.txt");

然后,创建一个整型数组。确保它可以容纳与您刚刚从文件中读取的行数一样多的 int 数组,因为这是您将保存结果的地方:

var result = new System.Int32[lines.Length][];

现在,从您阅读的每一行中,通过拆分该行并使用逗号字符作为分隔符来构造一个字符串数组:

for (var i = 0; i < lines.Length; i++)
{
    var str = lines[i].Split(',');

最后把那个字符串数组转成int数组存起来:

    result[i] = str.Select(System.Int32.Parse).ToArray();
}

我不会回答这个问题,但是因为已经有人回答了....

我会使用 Linq

var array = File.ReadLines(filename)
            .Select(line => line.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            .ToArray();
      string file = @"C:\Path";
        using (StreamReader SR = new StreamReader(file))
        {
            string line;
            while ((line = SR.ReadLine()) != null)
            {
                string[] Splitline = line.Split(',');
                // Do what you want with each array line
            }
        }