在CSV文件中添加的栏目,以鲜明的重点 [英] Adding up columns in a CSV file to a distinct Key

查看:124
本文介绍了在CSV文件中添加的栏目,以鲜明的重点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有拥有众多的行和列的CSV文件。所有行会,除了这将是一个帐户名的第一列是不同的。还有比如有每个帐户名称约有100行十个不同的帐户名。因此,对于出现我需要得到一笔该行中的所有程序列的每个帐户名。

I have a CSV file that has numerous rows and columns. All rows will be distinct except for the first column which will be an account name. There are for example ten different account names that have about 100 rows for each account name . So for each account name that appears I need to get a sum for all the proceeding columns in that row.

因此​​,一个CSV文件是这样的:

So a csv file like this:

史密斯,10,5,9

Smith, 10, 5, 9

史密斯,9,5,6

琼斯,10,5,7

琼斯,9,6,5

需要被写入到这样的另一个文件:

Needs to be written to another file like this:

·史密斯,19,19,15

Smith, 19, 19, 15

·琼斯,19,11,12

Jones, 19, 11, 12

一直试图使用数组或字典做这一切上午,但我似乎无法在逻辑上完成这件事。

Been trying all morning using either an array or a Dictionary to do this, but I can't seem to logically get this done.

    static void Main(string[] args)
    {
        String path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CsvReader\Position_2016_02_25.0415.csv";

        string[] lines = File.ReadAllLines(path);
        foreach(string line in lines)
        {
            string[] parsedLine = line.Split(',');
            //for each iteration through the loop where string[0] is already existing
            //I want to have sum = sum + string[1]
        }
        Console.Read();
    }   

我也使用字典这种尝试,但最终只抓住行时鲜明的名字出现了。

I also tried using a dictionary for this but ended up only grabbing the row when a distinct name came up.

//To add account as key and each line as value
foreach(var s in data)
{
    string[] temp = s.Split(',');
    //Adds key of account into dictionary, full string line as value.
    dictionary.Add(temp[0], temp);
    foreach (KeyValuePair<string, string[]> accountKeyValuePair in dictionary)
    {
        Console.WriteLine("Account = {}", accountKeyValuePair.Key);   //Additional information: Input string was not in a correct format.
    }
}

寻找在正确的逻辑方向或者是链接到一个类似的例子,或者一个温柔的微调。我真的不想要的答案codeD给我。

Looking for either a link to a similar example, or maybe a gentle nudge in the right logical direction. I don't really want the answer coded for me.

推荐答案

检查:

public static void Run()
        {
            var lines = new List<string>() { 
            "Smith, 10, 5, 9",
            "Smith, 9, 5, 6",
            "Jones, 10, 5, 7",
            "Jones, 9, 6, 5"
            };


            var aList = from l in lines                        
                        select new { Name = l.Split(',')[0], Value1 = Convert.ToInt32(l.Split(',')[1]), Value2 = Convert.ToInt32(l.Split(',')[2]), Value3 = Convert.ToInt32(l.Split(',')[3]) };

            var vList = from a in aList
                        group a by a.Name into g
                        select new { Name = g.Key, Value1Sum = g.Sum(a => a.Value1), Value2Sum = g.Sum(a => a.Value2), Value3Sum = g.Sum(a => a.Value3) };

            foreach (var v in vList)
                Console.WriteLine("{0} {1} {2} {3}", v.Name, v.Value1Sum, v.Value2Sum, v.Value3Sum);
        }

这篇关于在CSV文件中添加的栏目,以鲜明的重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆