从 CSV 文件中读取数字并计算平均值

Reading numbers from CSV file and calculating the average

我需要帮助从 CSV 文件中提取数字并计算平均值。到目前为止,我可以从最后一列中检索到正确的数字,但似乎我没有将它们转换为正确类型的数组。我想我要找的号码应该是Average = 6.4.

private void label21_Click(object sender, EventArgs e)
{
    var path = conf.path + "\" + "CloudOpen.csv"; 

    using (TextFieldParser csvParser = new TextFieldParser(path))
    {
        csvParser.CommentTokens = new string[] { "#" };
        csvParser.SetDelimiters(new string[] { "," });
        csvParser.HasFieldsEnclosedInQuotes = false;
        csvParser.ReadLine();

        while (!csvParser.EndOfData)
        {
            // Read current line fields, pointer moves to the next line.
            string[] fields = csvParser.ReadFields();
            string five = fields[5]; 
            var intArray = five.Select(c => c - '0').ToArray(); 

            Average(intArray);
        }
    }
}

public void Average(int[] array)
{
    double avg = Queryable.Average(array.AsQueryable());

    Console.WriteLine("Average = " + avg);
}

这是我正在阅读的 CSV 文件:

Id,time,two,five,ten,twenty
0,03/07/2022 14:47:03,0,1,2,5
0,03/07/2022 14:47:33,0,1,2,6
0,03/07/2022 14:48:37,0,1,3,6
0,03/07/2022 14:48:37,0,1,3,6
0,03/07/2022 14:48:37,0,1,3,7
0,03/07/2022 14:48:37,0,1,3,8

创建一个 .NET 6.0 命令行项目,您可以 运行 以下代码而不用担心命名空间,类 等。我相信您可能需要四舍五入到一位小数或使用您的用例上限?

//Add six values to list of integers (simplifying the CSV to Array code)
List<int> listInts = new List<int>() { 5, 6, 6, 6, 7, 8 };

double avg = Queryable.Average(listInts.AsQueryable()); //This variable will be a double of 6.333 repeating
double avgOneDecimal = Math.Round(avg, 1); //This variable will round to one decimal place
double avgOneDecimalCeiling = Math.Ceiling(avg); //This variable will be 7 if you need to round up for whatever reason

Console.WriteLine($"Average = {avg}");
Console.WriteLine($"Average to one decimal = {avgOneDecimal}");
Console.WriteLine($"Average rounded up = {avgOneDecimalCeiling}");
public void average()

        {

            List<double> listA = new List<double>();

            int five;

            var path = conf.path + "\" + "CloudOpen.csv"; 

            using (TextFieldParser csvParser = new TextFieldParser(path))

            {

                csvParser.CommentTokens = new string[] { "#" };

                csvParser.SetDelimiters(new string[] { "," });

                csvParser.HasFieldsEnclosedInQuotes = false;

                csvParser.ReadLine();

                while (!csvParser.EndOfData)

                {
                    string[] fields = csvParser.ReadFields();

                    five = fields[5].Count();
   
                    var fi = Convert.ToInt32(fields[5]);

                    listA.Add(fi);

                }

                int total = listA.Sum(x => Convert.ToInt32(x));

                var count = listA.Count;

                Console.WriteLine("Numbers added:" + total);

                Console.WriteLine("Number of items:" + listA.Count);

                Console.WriteLine(total / count);

            }

        }