0

I require to extract specific column's data from CSV file and output should also be a CSV format. I have gone through many solutions about extracting data from CSV but none of them talks about extracting it in CSV format itself

Example :

 public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

                string filePath = "abc.csv";

                string csvContnet = File.ReadAllText(filePath);

                // E.g. Suppose above csv has 9 columns
                // And column headers are from A,B,C...I
                // Now I want to select data from columheaders B,C and F along with header
                string requiredCsvContent = this.GetRequiredCsvContent(csvContnet);
            }

            /// <summary>
            /// Following function returns required CSV content from main csv File
            /// </summary>
            /// <param name="csvContnet"></param>
            /// <returns></returns>
            private string GetRequiredCsvContent(string csvContnet)
            {

            }}
5
  • 1
    Could you post an example? Commented Feb 18, 2014 at 12:39
  • Please go through edits Commented Feb 18, 2014 at 12:53
  • Could you clarify the example? You have 9 headers (A -> I) but only 7 columns? Commented Feb 18, 2014 at 12:55
  • Removed unwanted headers from above example Commented Feb 18, 2014 at 13:16
  • @Nilesh what should be the format of the output ? Commented Feb 19, 2014 at 4:41

3 Answers 3

1

Your data looks like a TSV (tab separated values) file. Just copy the whole thing into your clipboard and paste it into Excel. Then, delete all columns except D, E and F. Lastly, go to File > Save As > CSV.

Sign up to request clarification or add additional context in comments.

1 Comment

lol this answer is so simple, ingenious, and funny all in one +1
1

I would recommend you to use File.ReadLines or File.ReadAllLines so you can get what you want with LINQ easily:

var lines = File.ReadLines("asd")
              .Skip(1) // header
              .Select(line =>
              {
                  var parts = line.Split(',');
                  return string.Join(",", parts[1], parts[2], parts[5]);
              }).ToList();

That should return A,B and F columns in comma-seperated format.

Comments

0

If I'm reading it correctly, you want to extract the headers and data from columns B, C, and F, and put the output into CSV format within a string? Try this out, using IdComLog.Data (full disclosure, I am its author):

var data = from row in IdComLog.Data.Formats.Csv.ReadRecords("inputFile.csv")
           select new[]{ row[1].Value, row[2].Value, row[5].Value }

var sw = new StringWriter();
IdComLog.Data.Formats.Csv.Write(sw, data);

string requiredCsvContent = sw.ToString();

Highly recommend not using Split() or Join() to work with CSV. It will break on non-trivial data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.