Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to split a CSV by a source in the data but keep the headers. The orginal code I had was -


F#
var splitQuery = from line in File.ReadLines(@"C:\test\test1.csv")
            let source = line.Split(',').Last()
            group line by source into outputs
            select outputs;

foreach (var output in splitQuery)
{
    File.WriteAllLines(@"C:\test\" + output.Key + ".csv", output);
}



This worked however it didnt include the headers in the split files, so I have tried this -

C#
var headerLine = File.ReadLines(@"C:\test\test1.csv");
            var nHeaderLine = headerLine.Take(1);

            string FHeader = nHeaderLine.ToString();


            var splitQuery = from line in headerLine
                             let source = line.Split(',').Last()
                             group line by source into outputs
                             select outputs;

            foreach (var output in splitQuery)
            {
                var Foutput = FHeader + "\r\n" + output;

                File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);

            }



Im probably doing this completely wrong but im getting two errors (so far) and these are


C#
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);


C#
Error	1	The best overloaded method match for 'System.IO.File.WriteAllLines(string, System.Collections.Generic.IEnumerable<string>)' has some invalid arguments	C:\Users\nicoles\Documents\Visual Studio 2013\Projects\splitTest\splitTest\Program.cs	32	17	splitTest



C#
Error	2	Argument 2: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>'	C:\Users\nicoles\Documents\Visual Studio 2013\Projects\splitTest\splitTest\Program.cs	32	71	splitTest



Could someone help me out please

What I have tried:

C#
var headerLine = File.ReadLines(@"C:\test\test1.csv");
            var nHeaderLine = headerLine.Take(1);

            string FHeader = nHeaderLine.ToString();


            var splitQuery = from line in headerLine
                             let source = line.Split(',').Last()
                             group line by source into outputs
                             select outputs;

            foreach (var output in splitQuery)
            {
                var Foutput = FHeader + "\r\n" + output;

                File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);

            }
Posted
Updated 29-Sep-16 4:50am

I recently posted an article about parsing CSV files. It maintains the column names in a separate list called "Columns". The class is pretty lightweight and you can inherit the class and override pretty much all of the functionality to do what you need it to do.

CSV File Parser[^]

It doesn't write files, because it's primary purpose is to simply read CSV files and separate the fields into their respective parts. However, in your derived class, you can certain add that functionality.
 
Share this answer
 
v2
Comments
Maciej Los 29-Sep-16 13:59pm    
5ed!
WriteAllLines need collection of string as input, so you need to create a collection of string that contains all lines that needs to be saved in file and then at last call the WriteAllLines method write it just once like:

C#
var lines=  splitQuery.Select(output=> FHeader + "\r\n" + output)
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", lines);           


or more better can be to avoid extra linq query by doing :

C#
var lines = from line in headerLine
            let source = line.Split(',').Last()
            group line by source into outputs
            select FHeader + "\r\n" + outputs;

File.WriteAllLines(@"C:\test\" + output.Key + ".csv", lines); 
 
Share this answer
 
v3
See File.WriteAllLines Method (System.IO)[^]; it does not have an overload that takes two strings. And do you really want to write the header line before every text line in your output? Try using proper variable types, rather than using var everywhere, and it is probably easier to see what is wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900