无法分割字符串,并且令人困惑&;;&;要求

本文关键字:要求 分割 字符串 | 更新日期: 2023-09-27 18:13:49

下面是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace DataAnalysis
{
class Data
{
    public int InTime;
    public string InLocation;
    public bool Direction;
    public int LOS_F;
    // create a Data object from a CSV format string.
    static Data FromString(string line)
    {
        var fields = line.split(",");
        return new Data
        {
            InTime = TimeSpan.Parse(fields[3]),
            InLocation = fields[5],
            Direction = fields[5][0], // to get the direction E/N/S/W
            LOS_F = float.Parse(fields[16])
        };
    }
}
class Program
{
    string[] directions = new string[] { "E", "N", "S", "W" };
    static void Main(string[] args)
    {
        var path = @"C:'Documents and Settings'Siva-Admin'Desktop'5.5 Capacity Models'";
        //         ^--- No need to escape the backslashes
        var subdirs = Directory.GetDirectories(path);
        // The subdirs variable contains the FULL paths
        foreach (string subdir in subdirs)
        {
            List<List<float>> allAvgs = new List<List<float>>();
            using (StreamWriter compiled = new StreamWriter(
                   Path.Combine(subdir, "compiledresults.csv")))
            {
                compiled.Write("heading,EastAvg,NorthAvg,SouthAvg,WestAvg");
                for (int i = 1; i <= 10; i++)
                {
                    List<Data> info = new List<Data>();
                    using (StreamReader reader = new StreamReader(
                           Path.Combine(subdir, "results" + i.ToString() + @"'JourneyTimes.csv")))
                    {
                        // Read the header line first!
                        string line = reader.ReadLine();
                        while ((line = reader.ReadLine()) != null)
                            info.Add(Data.FromString(line));
                    }
                    List<float> avgs = new List<float>();
                    for (string dir in directions)
                    {
                        List<Data> perDirection = info.Where(d => d.Direction = dir) as List<Data>;
                        float sum = perDirection.Sum(d => d.LOS_F);
                        float average = sum / perDirection.Count();
                        avgs.Add(average);
                    }
                    allAvgs.Add(avgs);
                    compiled.Write("results" + i.ToString() + "," + string.Join(",", avgs) + "'n");
                }
                compiled.Write("scenario_average");
                for (int j = 1; j <= 4; j++)
                {
                    compiled.Write("," + allAvgs.Sum(d => d[0]) / allAvgs.Count());
                }
            }
        }
    }
}

}

得到以下错误:

Error 1; expected(Line 67, "for (string dir in directions)" )
Error 2; expected(LINE 67, "                            " )     
Error 3 'string' does not contain a definition for 'split' and no extension method 'split' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) (LINE 20, var fields = line.split (","); )

我不明白需要;,因为这些是传递的参数,不是吗?我也不明白为什么我不能分割字符串

无法分割字符串,并且令人困惑&;;&;要求

错误1和2: for循环应该是foreach循环:

foreach (string dir in directions)

错误3: split中的S应该是大写的:

var fields = line.Split(',');

for (string dir in directions)应为foreach (string dir in directions)

编辑添加:

柴鸡蛋,这里:

List<Data> perDirection = info.Where(d => d.Direction = dir) as List<Data>;

你似乎在做赋值而不是相等性检查,即

d.Direction = dir应为d.Direction == dir

然而,它仍然不能工作,因为你是比较字符串和bool。

您需要将line.split(",")替换为line.Split(',')(注意大写的S)。
(也像其他人指出的那样,将第67行中的for替换为foreach)。

Error1 &

使用foreach代替for;