0

1 つに統合しようとしている 2 つの CSV ファイルがあります。

A.CSV

WBS Element,Purchasing Document,Purchase order text,Val/COArea Crcy
ABC123,,,75000
ABC124,4200028630,Service,1069.2
ABC124,4200041490,Service,25518.24
ABC124,4200041490,Service,-1890.24
ABC126,4200028630,Service,2268
ABC126,4200028630,Service,-2268
ABC126,4200029435,Service,25149.65
ABC137,,,4146.2

B.CSV

WBS Element,Ref Document Number,Val/COArea Crcy,Name
ABC124,1000060610,0,Slab Locates & Steel Differential
ABC124,1000081223,0,NOCN339A&3921
ABC124,1000081223,0,Slab Locates & Steel Differential
ABC126,1000067757,0,Structural Steel
ABC 137,4200041490,0,Service
ABC 137,4200028630,5393.52,Service
ABC 137,4200029435,0,Service

この両方を組み合わせた1つのCSVファイルを作りたいです。WBS Element で始まる行が結合されます。各ファイルの WBS 要素は、一致する場合は同じ行に配置されます。A に WBS 要素 B がない場合、B のセクションは "," だけであり、その逆も同様です。

ターゲット出力の例:

WBS Element,Purchasing Document,Purchase order text,Val/COArea Crcy,WBS Element,Ref Document Number,Val/COArea Crcy,Name
ABC123,,,75000,,,,
ABC124,4200028630,Service,1069.2,ABC124,1000060610,0,Slab Locates & Steel Differential

次のコードがあります。

static void Main(string[] args)
    {
        StreamReader a = new StreamReader(@"Input\a.csv");
        StreamReader b = new StreamReader(@"Input\b.csv");
        StreamWriter output = new StreamWriter(@"Output\output.csv");
        Dictionary<string, string> Adict = new Dictionary<string, string>();
        Dictionary<string, string> Bdict = new Dictionary<string, string>();


        output.WriteLine(a.ReadLine() + "," + b.ReadLine());

        while (!a.EndOfStream && !b.EndOfStream)
        {
            //section for A
            List<string> atempList = new List<string>();
            string atempString;
            string Aline = a.ReadLine();
            string[] Atokens = Aline.Split(','); //split the line into array
            foreach (string s in Atokens)
                atempList.Add(s); //add each string in token array to tempList
            atempList.Remove(Atokens[0]); //remove Dict Key from tempList

            StringBuilder d = new StringBuilder();

            if (!Adict.ContainsKey(Atokens[0]))
            {
                foreach (string s in atempList)
                    d.Append(s + ","); //rejoin tempList into a string with ","
                d.Append("\n"); //add a linebreak to end of templist string
                Adict.Add(Atokens[0], d.ToString()); //Add line to dictionary with Key
            }
            else  //Adict does contain key... need to remove Key and add bigger string
            {
                List<string> removeKey = new List<string>(); //temporary list

                foreach (string s in Atokens)
                    removeKey.Add(s); //create a new list from the token array
                removeKey.Remove(Atokens[0]); //remove the key from the removeKey list

                atempString = Adict[Atokens[0]];  //temporary string is what's already in dictionary
                Adict.Remove(Atokens[0]); //remove the Key + Value from dictionary.
                Adict.Add(Atokens[0], d.Append(atempString + Aline + "\n").ToString());     // string.Concat(tempString, ",", line));
            }

            //section for B
            List<string> btempList = new List<string>();
            string btempString;
            string Bline = b.ReadLine();
            string[] Btokens = Bline.Split(',');
            foreach (string s in Btokens)
                btempList.Add(s);
            btempList.Remove(Btokens[0]);

            StringBuilder f = new StringBuilder();

            if (!Bdict.ContainsKey(Btokens[0]))
            {
                foreach (string s in btempList)
                    f.Append(s + ",");
                f.Append("\n");
                Bdict.Add(Btokens[0], f.ToString());
            }
            else
            {
                List<string> removeKey = new List<string>();

                foreach (string s in Btokens)
                    removeKey.Add(s);
                removeKey.Remove(Atokens[0]);

                btempString = Bdict[Btokens[0]];
                Bdict.Remove(Btokens[0]);
                Bdict.Add(Btokens[0], f.Append(btempString + Bline + "\n").ToString());
            }
        }
        output.Close();
       // Console.ReadLine();
    }
}

各辞書を調べてキーを比較し、一致するキーを持つ行だけを結合 (挿入?) する方法がわかりません。

4

1 に答える 1