无法使用 .Split (C# Unity3D) 将数据从文本文件转换为数组

Cannot convert data from textfile to array using .Split (C# Unity3D)

我有一个(也许)简单的问题,关于在 Unity (C#) 中读取包含浮点矩阵的文本文件,并将其转换为浮点矩阵(或多个数组)。在到达那里之前,我似乎无法使用 .Split 方法将 'string' 类型的文本成功转换为数组。

总而言之,我需要在我的 Unity 脚本中使用在 MATLAB 中训练的人工神经网络的权重来计算输出和执行某些操作。

我会仔细向你展示我的尝试;首先,我打算读取的 "WeightsIH.txt" 文件如下所示:

-0.493654767886117  1.96685485682002
-0.493654767886117  1.96685485682002
-1.12167737625244   -0.835107290621868
-0.168882176970878  -0.0508678270221517
0.00848314746590752 0.645890421329432
-0.445148017062813  -0.647593931130814
0.0719861441229913  0.00251854104000363
-0.0809087627607675 -0.00253116474618752
0.0677112130287254  0.00229085080614581
0.0754386783608645  0.00239167974789985
0.0809669296303145  0.00253343860819182
-3.54887781611766   -0.884835613897628
-0.459886351851150  -0.848445431434901
-0.0670274486060166 -1.39397672397051
-3.82077442331056   -2.40290337409384
0.0783082838340459  0.00245132734091342
-0.239255503684809  -0.0118048053697862
-0.0798562101865881 -0.00249224979249840
-0.0639184016149756 -0.00224822783089479
-0.778070988555323  -0.872732391008980
0.0297507291420014  -1.74952458318966
0.0963966379119354  0.00416637970011285
0.875794416782941   0.513267873404847
0.0788049456605797  0.00246400296768071
-0.301882135742981  1.29009004735214
-0.427112778202037  -0.602081710823938
-0.0287160010856207 0.876736618413824
0.174484840994587   -0.914135602108887
-1.13771093704277   -1.80803211709460
-0.842897124110693  -0.491320433352398
-0.883862027266628  0.577867664170583
-0.00732465337711950    -0.0608133682721268
0.0808154577205481  0.00252756604265255
-0.623653482256716  0.802021714172196
0.354715179032082   -1.40951283673210
0.107130434979695   0.00718795362864938
-3.25429700675793   1.15179026184665
0.00323132998826740 0.725967105940923
-0.445271160376852  -0.634848835782688
0.0353267566953751  -0.761005808087746
0.343818927585420   0.181552084533698
1.52333372694938    -1.95500299702055
1.28692979700544    2.03963829780562
0.665570336193150   -0.410729910590931
-0.0861536175683719 -0.00286332759826070
0.126619076047933   0.0171533754647685
-0.0822566525595005 -0.00259193055392493
-1.28712374245194   1.12380947288866
-1.29253445353219   -2.05175940341935
0.416493102590467   -0.617909454448863
0.0969179981825367  0.182721981022912
-0.0808788394332979 -0.00252999992128388
0.925818615324062   -1.91710736361040
-0.438361270919922  0.0119396635243124
1.05250770533487    -0.965588245752274
-0.0480257526132498 -0.00154845733604858
-0.0586872685404968 -0.00184430469780941
-0.471992797137374  -0.672492593551730
0.439729645530884   1.55878656878788
1.68156076364744    1.32277838623733
-0.455916176089339  -0.632974493608950
-2.76038741468600   1.87628535124240
0.993963121269136   0.412382925821147
0.0813294944564905  0.00254834378164385
1.05785147371486    -0.713165079912121
0.542621317508334   0.263699015691544
0.0859471661820603  0.00284559667679037
0.0752254002940845  0.00264837949098051
-0.0821494531270125 -0.00258646752960803
-0.135286303232541  -0.0230503985166856
-1.04824146276167   0.379479302836832
-1.00411135626130   0.643815076295448
-1.06427192746705   -1.71485567599474
0.0306923117644997  -0.326399702175058
-0.269230352435044  1.15492815472443
-1.09071040094221   0.974587786723127
-0.0811137162377503 -0.00253932111442298
0.843300471645878   -0.443547135621555
2.62543922875650    -1.43287453338882
-0.0879222032042109 -0.00305697486042674
1.08943207983567    -0.751402936369758
-0.0807147111768526 -0.00252376120454932
0.0920673615786699  0.00345537914425264
-3.32572250550751   2.23334579177979
0.567347532732561   -0.849919751538726
-0.981291377531284  -1.65375737600341
0.717607651399507   -0.501535458162733

现在我的 C# Unity 代码如下所示:

using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class WeightsMDF : MonoBehaviour
{

    private string text;
    void Start ()
    {
        FileInfo theSourceFile = new FileInfo 
        ("C:/Users/Ajdin/Downloads/UnitySpace/minorProject/Nerd/Nerds/Assets/Scenes/WeightsIH.txt");
        StreamReader reader = theSourceFile.OpenText();

        do
        {
            text = reader.ReadLine();
            string[] floats = text.Split(" "[0]);
            //Console.WriteLine(text);
            print (text); 
            //print(floats[0]);
        } while (text != null);   

    }
}

现在这段代码在控制台中为我提供了整个文本文件,但最后带有一个空引用异常: NullReferenceException: 对象引用未设置到对象的实例 WeightsMDF.Start () (位于 Assets/Scripts/Nerds/WeightsMDF.cs:19).

这个异常是指第19行:

string[] floats = text.Split(" "[0]);

现在,如果我评论这一行,我会得到相同的输出,除了最后一个控制台输出行说

Null

如果我将第 19 行更改为:

string[] floats = text.Split(" ");//[0]);

我得到以下编译错误:

Assets/Scripts/Nerds/WeightsMDF.cs(19,36): error CS1502: The best overloaded method match for `string.Split(params char[])' has some invalid arguments    
Assets/Scripts/Nerds/WeightsMDF.cs(19,36): error CS1503: Argument `#1' cannot convert `string' expression to type `char[]'

有谁知道我做错了什么? 提前致谢!

尝试在使用拆分之前检查文本。 ReadLine returns null 在文件末尾,所以你需要在拆分之前检查它。

text = reader.ReadLine();
if (text == null) 
     break;

string[] floats = text.Split(" "[0]);

此外,您可以使用 Peek。它 returns 下一个符号并且不使用它并且 -1 如果没有剩余符号。

        using (StreamReader sr = new StreamReader(path)) 
        {
            while (sr.Peek() >= 0) 
            {
                text = sr.ReadLine();
                var splits = text.Split (new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
            }
        }

要按空格拆分字符串,您可以使用

string[] splits = text.Split(null);
string[] splits = text.Split(new char[0]);

代替 do while 循环,使用条件为 while 的循环:(!reader.EndOfStream).

对于拆分问题,您需要将字符而不是字符串作为参数发送。用 ' ' 而不是 " " 像:

string[] floats = text.split(' ');